file.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. * Regular file handling primitives for NTFS-based filesystems.
  7. *
  8. */
  9. #include <linux/backing-dev.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/compat.h>
  13. #include <linux/falloc.h>
  14. #include <linux/fiemap.h>
  15. #include <linux/fileattr.h>
  16. #include "debug.h"
  17. #include "ntfs.h"
  18. #include "ntfs_fs.h"
  19. static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
  20. {
  21. struct fstrim_range __user *user_range;
  22. struct fstrim_range range;
  23. struct block_device *dev;
  24. int err;
  25. if (!capable(CAP_SYS_ADMIN))
  26. return -EPERM;
  27. dev = sbi->sb->s_bdev;
  28. if (!bdev_max_discard_sectors(dev))
  29. return -EOPNOTSUPP;
  30. user_range = (struct fstrim_range __user *)arg;
  31. if (copy_from_user(&range, user_range, sizeof(range)))
  32. return -EFAULT;
  33. range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
  34. err = ntfs_trim_fs(sbi, &range);
  35. if (err < 0)
  36. return err;
  37. if (copy_to_user(user_range, &range, sizeof(range)))
  38. return -EFAULT;
  39. return 0;
  40. }
  41. /*
  42. * ntfs_fileattr_get - inode_operations::fileattr_get
  43. */
  44. int ntfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  45. {
  46. struct inode *inode = d_inode(dentry);
  47. struct ntfs_inode *ni = ntfs_i(inode);
  48. u32 flags = 0;
  49. if (inode->i_flags & S_IMMUTABLE)
  50. flags |= FS_IMMUTABLE_FL;
  51. if (inode->i_flags & S_APPEND)
  52. flags |= FS_APPEND_FL;
  53. if (is_compressed(ni))
  54. flags |= FS_COMPR_FL;
  55. if (is_encrypted(ni))
  56. flags |= FS_ENCRYPT_FL;
  57. fileattr_fill_flags(fa, flags);
  58. return 0;
  59. }
  60. /*
  61. * ntfs_fileattr_set - inode_operations::fileattr_set
  62. */
  63. int ntfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry,
  64. struct fileattr *fa)
  65. {
  66. struct inode *inode = d_inode(dentry);
  67. struct ntfs_inode *ni = ntfs_i(inode);
  68. u32 flags = fa->flags;
  69. unsigned int new_fl = 0;
  70. if (fileattr_has_fsx(fa))
  71. return -EOPNOTSUPP;
  72. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_COMPR_FL))
  73. return -EOPNOTSUPP;
  74. if (flags & FS_IMMUTABLE_FL)
  75. new_fl |= S_IMMUTABLE;
  76. if (flags & FS_APPEND_FL)
  77. new_fl |= S_APPEND;
  78. /* Allowed to change compression for empty files and for directories only. */
  79. if (!is_dedup(ni) && !is_encrypted(ni) &&
  80. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  81. int err = 0;
  82. struct address_space *mapping = inode->i_mapping;
  83. /* write out all data and wait. */
  84. filemap_invalidate_lock(mapping);
  85. err = filemap_write_and_wait(mapping);
  86. if (err >= 0) {
  87. /* Change compress state. */
  88. bool compr = flags & FS_COMPR_FL;
  89. err = ni_set_compress(inode, compr);
  90. /* For files change a_ops too. */
  91. if (!err)
  92. mapping->a_ops = compr ? &ntfs_aops_cmpr :
  93. &ntfs_aops;
  94. }
  95. filemap_invalidate_unlock(mapping);
  96. if (err)
  97. return err;
  98. }
  99. inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
  100. inode_set_ctime_current(inode);
  101. mark_inode_dirty(inode);
  102. return 0;
  103. }
  104. /*
  105. * ntfs_ioctl - file_operations::unlocked_ioctl
  106. */
  107. long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
  108. {
  109. struct inode *inode = file_inode(filp);
  110. struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
  111. switch (cmd) {
  112. case FITRIM:
  113. return ntfs_ioctl_fitrim(sbi, arg);
  114. }
  115. return -ENOTTY; /* Inappropriate ioctl for device. */
  116. }
  117. #ifdef CONFIG_COMPAT
  118. long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
  119. {
  120. return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  121. }
  122. #endif
  123. /*
  124. * ntfs_getattr - inode_operations::getattr
  125. */
  126. int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
  127. struct kstat *stat, u32 request_mask, u32 flags)
  128. {
  129. struct inode *inode = d_inode(path->dentry);
  130. struct ntfs_inode *ni = ntfs_i(inode);
  131. stat->result_mask |= STATX_BTIME;
  132. stat->btime = ni->i_crtime;
  133. stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
  134. if (inode->i_flags & S_IMMUTABLE)
  135. stat->attributes |= STATX_ATTR_IMMUTABLE;
  136. if (inode->i_flags & S_APPEND)
  137. stat->attributes |= STATX_ATTR_APPEND;
  138. if (is_compressed(ni))
  139. stat->attributes |= STATX_ATTR_COMPRESSED;
  140. if (is_encrypted(ni))
  141. stat->attributes |= STATX_ATTR_ENCRYPTED;
  142. stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
  143. STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
  144. generic_fillattr(idmap, request_mask, inode, stat);
  145. return 0;
  146. }
  147. static int ntfs_extend_initialized_size(struct file *file,
  148. struct ntfs_inode *ni,
  149. const loff_t valid,
  150. const loff_t new_valid)
  151. {
  152. struct inode *inode = &ni->vfs_inode;
  153. struct address_space *mapping = inode->i_mapping;
  154. struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
  155. loff_t pos = valid;
  156. int err;
  157. if (is_resident(ni)) {
  158. ni->i_valid = new_valid;
  159. return 0;
  160. }
  161. WARN_ON(is_compressed(ni));
  162. WARN_ON(valid >= new_valid);
  163. for (;;) {
  164. u32 zerofrom, len;
  165. struct folio *folio;
  166. u8 bits;
  167. CLST vcn, lcn, clen;
  168. if (is_sparsed(ni)) {
  169. bits = sbi->cluster_bits;
  170. vcn = pos >> bits;
  171. err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL,
  172. false);
  173. if (err)
  174. goto out;
  175. if (lcn == SPARSE_LCN) {
  176. pos = ((loff_t)clen + vcn) << bits;
  177. ni->i_valid = pos;
  178. goto next;
  179. }
  180. }
  181. zerofrom = pos & (PAGE_SIZE - 1);
  182. len = PAGE_SIZE - zerofrom;
  183. if (pos + len > new_valid)
  184. len = new_valid - pos;
  185. err = ntfs_write_begin(file, mapping, pos, len, &folio, NULL);
  186. if (err)
  187. goto out;
  188. folio_zero_range(folio, zerofrom, folio_size(folio) - zerofrom);
  189. err = ntfs_write_end(file, mapping, pos, len, len, folio, NULL);
  190. if (err < 0)
  191. goto out;
  192. pos += len;
  193. next:
  194. if (pos >= new_valid)
  195. break;
  196. balance_dirty_pages_ratelimited(mapping);
  197. cond_resched();
  198. }
  199. return 0;
  200. out:
  201. ni->i_valid = valid;
  202. ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
  203. new_valid);
  204. return err;
  205. }
  206. /*
  207. * ntfs_zero_range - Helper function for punch_hole.
  208. *
  209. * It zeroes a range [vbo, vbo_to).
  210. */
  211. static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
  212. {
  213. int err = 0;
  214. struct address_space *mapping = inode->i_mapping;
  215. u32 blocksize = i_blocksize(inode);
  216. pgoff_t idx = vbo >> PAGE_SHIFT;
  217. u32 from = vbo & (PAGE_SIZE - 1);
  218. pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
  219. loff_t page_off;
  220. struct buffer_head *head, *bh;
  221. u32 bh_next, bh_off, to;
  222. sector_t iblock;
  223. struct folio *folio;
  224. bool dirty = false;
  225. for (; idx < idx_end; idx += 1, from = 0) {
  226. page_off = (loff_t)idx << PAGE_SHIFT;
  227. to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) :
  228. PAGE_SIZE;
  229. iblock = page_off >> inode->i_blkbits;
  230. folio = __filemap_get_folio(
  231. mapping, idx, FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  232. mapping_gfp_constraint(mapping, ~__GFP_FS));
  233. if (IS_ERR(folio))
  234. return PTR_ERR(folio);
  235. head = folio_buffers(folio);
  236. if (!head)
  237. head = create_empty_buffers(folio, blocksize, 0);
  238. bh = head;
  239. bh_off = 0;
  240. do {
  241. bh_next = bh_off + blocksize;
  242. if (bh_next <= from || bh_off >= to)
  243. continue;
  244. if (!buffer_mapped(bh)) {
  245. ntfs_get_block(inode, iblock, bh, 0);
  246. /* Unmapped? It's a hole - nothing to do. */
  247. if (!buffer_mapped(bh))
  248. continue;
  249. }
  250. /* Ok, it's mapped. Make sure it's up-to-date. */
  251. if (folio_test_uptodate(folio))
  252. set_buffer_uptodate(bh);
  253. else if (bh_read(bh, 0) < 0) {
  254. err = -EIO;
  255. folio_unlock(folio);
  256. folio_put(folio);
  257. goto out;
  258. }
  259. mark_buffer_dirty(bh);
  260. } while (bh_off = bh_next, iblock += 1,
  261. head != (bh = bh->b_this_page));
  262. folio_zero_segment(folio, from, to);
  263. dirty = true;
  264. folio_unlock(folio);
  265. folio_put(folio);
  266. cond_resched();
  267. }
  268. out:
  269. if (dirty)
  270. mark_inode_dirty(inode);
  271. return err;
  272. }
  273. /*
  274. * ntfs_file_mmap - file_operations::mmap
  275. */
  276. static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  277. {
  278. struct inode *inode = file_inode(file);
  279. struct ntfs_inode *ni = ntfs_i(inode);
  280. u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
  281. bool rw = vma->vm_flags & VM_WRITE;
  282. int err;
  283. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  284. return -EIO;
  285. if (is_encrypted(ni)) {
  286. ntfs_inode_warn(inode, "mmap encrypted not supported");
  287. return -EOPNOTSUPP;
  288. }
  289. if (is_dedup(ni)) {
  290. ntfs_inode_warn(inode, "mmap deduplicated not supported");
  291. return -EOPNOTSUPP;
  292. }
  293. if (is_compressed(ni) && rw) {
  294. ntfs_inode_warn(inode, "mmap(write) compressed not supported");
  295. return -EOPNOTSUPP;
  296. }
  297. if (rw) {
  298. u64 to = min_t(loff_t, i_size_read(inode),
  299. from + vma->vm_end - vma->vm_start);
  300. if (is_sparsed(ni)) {
  301. /* Allocate clusters for rw map. */
  302. struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
  303. CLST lcn, len;
  304. CLST vcn = from >> sbi->cluster_bits;
  305. CLST end = bytes_to_cluster(sbi, to);
  306. bool new;
  307. for (; vcn < end; vcn += len) {
  308. err = attr_data_get_block(ni, vcn, 1, &lcn,
  309. &len, &new, true);
  310. if (err)
  311. goto out;
  312. }
  313. }
  314. if (ni->i_valid < to) {
  315. if (!inode_trylock(inode)) {
  316. err = -EAGAIN;
  317. goto out;
  318. }
  319. err = ntfs_extend_initialized_size(file, ni,
  320. ni->i_valid, to);
  321. inode_unlock(inode);
  322. if (err)
  323. goto out;
  324. }
  325. }
  326. err = generic_file_mmap(file, vma);
  327. out:
  328. return err;
  329. }
  330. static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
  331. struct file *file)
  332. {
  333. struct ntfs_inode *ni = ntfs_i(inode);
  334. struct address_space *mapping = inode->i_mapping;
  335. loff_t end = pos + count;
  336. bool extend_init = file && pos > ni->i_valid;
  337. int err;
  338. if (end <= inode->i_size && !extend_init)
  339. return 0;
  340. /* Mark rw ntfs as dirty. It will be cleared at umount. */
  341. ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
  342. if (end > inode->i_size) {
  343. err = ntfs_set_size(inode, end);
  344. if (err)
  345. goto out;
  346. }
  347. if (extend_init && !is_compressed(ni)) {
  348. WARN_ON(ni->i_valid >= pos);
  349. err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
  350. if (err)
  351. goto out;
  352. } else {
  353. err = 0;
  354. }
  355. if (file && is_sparsed(ni)) {
  356. /*
  357. * This code optimizes large writes to sparse file.
  358. * TODO: merge this fragment with fallocate fragment.
  359. */
  360. struct ntfs_sb_info *sbi = ni->mi.sbi;
  361. CLST vcn = pos >> sbi->cluster_bits;
  362. CLST cend = bytes_to_cluster(sbi, end);
  363. CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
  364. CLST lcn, clen;
  365. bool new;
  366. if (cend_v > cend)
  367. cend_v = cend;
  368. /*
  369. * Allocate and zero new clusters.
  370. * Zeroing these clusters may be too long.
  371. */
  372. for (; vcn < cend_v; vcn += clen) {
  373. err = attr_data_get_block(ni, vcn, cend_v - vcn, &lcn,
  374. &clen, &new, true);
  375. if (err)
  376. goto out;
  377. }
  378. /*
  379. * Allocate but not zero new clusters.
  380. */
  381. for (; vcn < cend; vcn += clen) {
  382. err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
  383. &clen, &new, false);
  384. if (err)
  385. goto out;
  386. }
  387. }
  388. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  389. mark_inode_dirty(inode);
  390. if (IS_SYNC(inode)) {
  391. int err2;
  392. err = filemap_fdatawrite_range(mapping, pos, end - 1);
  393. err2 = sync_mapping_buffers(mapping);
  394. if (!err)
  395. err = err2;
  396. err2 = write_inode_now(inode, 1);
  397. if (!err)
  398. err = err2;
  399. if (!err)
  400. err = filemap_fdatawait_range(mapping, pos, end - 1);
  401. }
  402. out:
  403. return err;
  404. }
  405. static int ntfs_truncate(struct inode *inode, loff_t new_size)
  406. {
  407. struct super_block *sb = inode->i_sb;
  408. struct ntfs_inode *ni = ntfs_i(inode);
  409. int err, dirty = 0;
  410. u64 new_valid;
  411. if (!S_ISREG(inode->i_mode))
  412. return 0;
  413. if (is_compressed(ni)) {
  414. if (ni->i_valid > new_size)
  415. ni->i_valid = new_size;
  416. } else {
  417. err = block_truncate_page(inode->i_mapping, new_size,
  418. ntfs_get_block);
  419. if (err)
  420. return err;
  421. }
  422. new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
  423. truncate_setsize(inode, new_size);
  424. ni_lock(ni);
  425. down_write(&ni->file.run_lock);
  426. err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
  427. &new_valid, ni->mi.sbi->options->prealloc, NULL);
  428. up_write(&ni->file.run_lock);
  429. if (new_valid < ni->i_valid)
  430. ni->i_valid = new_valid;
  431. ni_unlock(ni);
  432. ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
  433. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  434. if (!IS_DIRSYNC(inode)) {
  435. dirty = 1;
  436. } else {
  437. err = ntfs_sync_inode(inode);
  438. if (err)
  439. return err;
  440. }
  441. if (dirty)
  442. mark_inode_dirty(inode);
  443. /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
  444. return 0;
  445. }
  446. /*
  447. * ntfs_fallocate - file_operations::ntfs_fallocate
  448. *
  449. * Preallocate space for a file. This implements ntfs's fallocate file
  450. * operation, which gets called from sys_fallocate system call. User
  451. * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
  452. * we just allocate clusters without zeroing them out. Otherwise we
  453. * allocate and zero out clusters via an expanding truncate.
  454. */
  455. static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
  456. {
  457. struct inode *inode = file_inode(file);
  458. struct address_space *mapping = inode->i_mapping;
  459. struct super_block *sb = inode->i_sb;
  460. struct ntfs_sb_info *sbi = sb->s_fs_info;
  461. struct ntfs_inode *ni = ntfs_i(inode);
  462. loff_t end = vbo + len;
  463. loff_t vbo_down = round_down(vbo, max_t(unsigned long,
  464. sbi->cluster_size, PAGE_SIZE));
  465. bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
  466. loff_t i_size, new_size;
  467. bool map_locked;
  468. int err;
  469. /* No support for dir. */
  470. if (!S_ISREG(inode->i_mode))
  471. return -EOPNOTSUPP;
  472. /*
  473. * vfs_fallocate checks all possible combinations of mode.
  474. * Do additional checks here before ntfs_set_state(dirty).
  475. */
  476. if (mode & FALLOC_FL_PUNCH_HOLE) {
  477. if (!is_supported_holes)
  478. return -EOPNOTSUPP;
  479. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  480. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  481. if (!is_supported_holes)
  482. return -EOPNOTSUPP;
  483. } else if (mode &
  484. ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  485. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
  486. ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
  487. mode);
  488. return -EOPNOTSUPP;
  489. }
  490. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  491. inode_lock(inode);
  492. i_size = inode->i_size;
  493. new_size = max(end, i_size);
  494. map_locked = false;
  495. if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
  496. /* Should never be here, see ntfs_file_open. */
  497. err = -EOPNOTSUPP;
  498. goto out;
  499. }
  500. if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
  501. FALLOC_FL_INSERT_RANGE)) {
  502. inode_dio_wait(inode);
  503. filemap_invalidate_lock(mapping);
  504. map_locked = true;
  505. }
  506. if (mode & FALLOC_FL_PUNCH_HOLE) {
  507. u32 frame_size;
  508. loff_t mask, vbo_a, end_a, tmp;
  509. err = filemap_write_and_wait_range(mapping, vbo_down,
  510. LLONG_MAX);
  511. if (err)
  512. goto out;
  513. truncate_pagecache(inode, vbo_down);
  514. ni_lock(ni);
  515. err = attr_punch_hole(ni, vbo, len, &frame_size);
  516. ni_unlock(ni);
  517. if (!err)
  518. goto ok;
  519. if (err != E_NTFS_NOTALIGNED)
  520. goto out;
  521. /* Process not aligned punch. */
  522. err = 0;
  523. mask = frame_size - 1;
  524. vbo_a = (vbo + mask) & ~mask;
  525. end_a = end & ~mask;
  526. tmp = min(vbo_a, end);
  527. if (tmp > vbo) {
  528. err = ntfs_zero_range(inode, vbo, tmp);
  529. if (err)
  530. goto out;
  531. }
  532. if (vbo < end_a && end_a < end) {
  533. err = ntfs_zero_range(inode, end_a, end);
  534. if (err)
  535. goto out;
  536. }
  537. /* Aligned punch_hole */
  538. if (end_a > vbo_a) {
  539. ni_lock(ni);
  540. err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
  541. ni_unlock(ni);
  542. if (err)
  543. goto out;
  544. }
  545. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  546. /*
  547. * Write tail of the last page before removed range since
  548. * it will get removed from the page cache below.
  549. */
  550. err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
  551. if (err)
  552. goto out;
  553. /*
  554. * Write data that will be shifted to preserve them
  555. * when discarding page cache below.
  556. */
  557. err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
  558. if (err)
  559. goto out;
  560. truncate_pagecache(inode, vbo_down);
  561. ni_lock(ni);
  562. err = attr_collapse_range(ni, vbo, len);
  563. ni_unlock(ni);
  564. if (err)
  565. goto out;
  566. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  567. /* Check new size. */
  568. err = inode_newsize_ok(inode, new_size);
  569. if (err)
  570. goto out;
  571. /* Write out all dirty pages. */
  572. err = filemap_write_and_wait_range(mapping, vbo_down,
  573. LLONG_MAX);
  574. if (err)
  575. goto out;
  576. truncate_pagecache(inode, vbo_down);
  577. ni_lock(ni);
  578. err = attr_insert_range(ni, vbo, len);
  579. ni_unlock(ni);
  580. if (err)
  581. goto out;
  582. } else {
  583. /* Check new size. */
  584. u8 cluster_bits = sbi->cluster_bits;
  585. /* Be sure file is non resident. */
  586. if (is_resident(ni)) {
  587. ni_lock(ni);
  588. err = attr_force_nonresident(ni);
  589. ni_unlock(ni);
  590. if (err)
  591. goto out;
  592. }
  593. /* generic/213: expected -ENOSPC instead of -EFBIG. */
  594. if (!is_supported_holes) {
  595. loff_t to_alloc = new_size - inode_get_bytes(inode);
  596. if (to_alloc > 0 &&
  597. (to_alloc >> cluster_bits) >
  598. wnd_zeroes(&sbi->used.bitmap)) {
  599. err = -ENOSPC;
  600. goto out;
  601. }
  602. }
  603. err = inode_newsize_ok(inode, new_size);
  604. if (err)
  605. goto out;
  606. if (new_size > i_size) {
  607. /*
  608. * Allocate clusters, do not change 'valid' size.
  609. */
  610. err = ntfs_set_size(inode, new_size);
  611. if (err)
  612. goto out;
  613. }
  614. if (is_supported_holes) {
  615. CLST vcn = vbo >> cluster_bits;
  616. CLST cend = bytes_to_cluster(sbi, end);
  617. CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
  618. CLST lcn, clen;
  619. bool new;
  620. if (cend_v > cend)
  621. cend_v = cend;
  622. /*
  623. * Allocate and zero new clusters.
  624. * Zeroing these clusters may be too long.
  625. */
  626. for (; vcn < cend_v; vcn += clen) {
  627. err = attr_data_get_block(ni, vcn, cend_v - vcn,
  628. &lcn, &clen, &new,
  629. true);
  630. if (err)
  631. goto out;
  632. }
  633. /*
  634. * Allocate but not zero new clusters.
  635. */
  636. for (; vcn < cend; vcn += clen) {
  637. err = attr_data_get_block(ni, vcn, cend - vcn,
  638. &lcn, &clen, &new,
  639. false);
  640. if (err)
  641. goto out;
  642. }
  643. }
  644. if (mode & FALLOC_FL_KEEP_SIZE) {
  645. ni_lock(ni);
  646. /* True - Keep preallocated. */
  647. err = attr_set_size(ni, ATTR_DATA, NULL, 0,
  648. &ni->file.run, i_size, &ni->i_valid,
  649. true, NULL);
  650. ni_unlock(ni);
  651. if (err)
  652. goto out;
  653. } else if (new_size > i_size) {
  654. i_size_write(inode, new_size);
  655. }
  656. }
  657. ok:
  658. err = file_modified(file);
  659. if (err)
  660. goto out;
  661. out:
  662. if (map_locked)
  663. filemap_invalidate_unlock(mapping);
  664. if (!err) {
  665. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  666. mark_inode_dirty(inode);
  667. }
  668. inode_unlock(inode);
  669. return err;
  670. }
  671. /*
  672. * ntfs_setattr - inode_operations::setattr
  673. */
  674. int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  675. struct iattr *attr)
  676. {
  677. struct inode *inode = d_inode(dentry);
  678. struct ntfs_inode *ni = ntfs_i(inode);
  679. u32 ia_valid = attr->ia_valid;
  680. umode_t mode = inode->i_mode;
  681. int err;
  682. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  683. return -EIO;
  684. err = setattr_prepare(idmap, dentry, attr);
  685. if (err)
  686. goto out;
  687. if (ia_valid & ATTR_SIZE) {
  688. loff_t newsize, oldsize;
  689. if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
  690. /* Should never be here, see ntfs_file_open(). */
  691. err = -EOPNOTSUPP;
  692. goto out;
  693. }
  694. inode_dio_wait(inode);
  695. oldsize = i_size_read(inode);
  696. newsize = attr->ia_size;
  697. if (newsize <= oldsize)
  698. err = ntfs_truncate(inode, newsize);
  699. else
  700. err = ntfs_extend(inode, newsize, 0, NULL);
  701. if (err)
  702. goto out;
  703. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  704. i_size_write(inode, newsize);
  705. }
  706. setattr_copy(idmap, inode, attr);
  707. if (mode != inode->i_mode) {
  708. err = ntfs_acl_chmod(idmap, dentry);
  709. if (err)
  710. goto out;
  711. /* Linux 'w' -> Windows 'ro'. */
  712. if (0222 & inode->i_mode)
  713. ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
  714. else
  715. ni->std_fa |= FILE_ATTRIBUTE_READONLY;
  716. }
  717. if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
  718. ntfs_save_wsl_perm(inode, NULL);
  719. mark_inode_dirty(inode);
  720. out:
  721. return err;
  722. }
  723. /*
  724. * check_read_restriction:
  725. * common code for ntfs_file_read_iter and ntfs_file_splice_read
  726. */
  727. static int check_read_restriction(struct inode *inode)
  728. {
  729. struct ntfs_inode *ni = ntfs_i(inode);
  730. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  731. return -EIO;
  732. if (is_encrypted(ni)) {
  733. ntfs_inode_warn(inode, "encrypted i/o not supported");
  734. return -EOPNOTSUPP;
  735. }
  736. #ifndef CONFIG_NTFS3_LZX_XPRESS
  737. if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
  738. ntfs_inode_warn(
  739. inode,
  740. "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
  741. return -EOPNOTSUPP;
  742. }
  743. #endif
  744. if (is_dedup(ni)) {
  745. ntfs_inode_warn(inode, "read deduplicated not supported");
  746. return -EOPNOTSUPP;
  747. }
  748. return 0;
  749. }
  750. /*
  751. * ntfs_file_read_iter - file_operations::read_iter
  752. */
  753. static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  754. {
  755. struct file *file = iocb->ki_filp;
  756. struct inode *inode = file_inode(file);
  757. struct ntfs_inode *ni = ntfs_i(inode);
  758. ssize_t err;
  759. err = check_read_restriction(inode);
  760. if (err)
  761. return err;
  762. if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
  763. ntfs_inode_warn(inode, "direct i/o + compressed not supported");
  764. return -EOPNOTSUPP;
  765. }
  766. return generic_file_read_iter(iocb, iter);
  767. }
  768. /*
  769. * ntfs_file_splice_read - file_operations::splice_read
  770. */
  771. static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
  772. struct pipe_inode_info *pipe, size_t len,
  773. unsigned int flags)
  774. {
  775. struct inode *inode = file_inode(in);
  776. ssize_t err;
  777. err = check_read_restriction(inode);
  778. if (err)
  779. return err;
  780. return filemap_splice_read(in, ppos, pipe, len, flags);
  781. }
  782. /*
  783. * ntfs_get_frame_pages
  784. *
  785. * Return: Array of locked pages.
  786. */
  787. static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
  788. struct page **pages, u32 pages_per_frame,
  789. bool *frame_uptodate)
  790. {
  791. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  792. u32 npages;
  793. *frame_uptodate = true;
  794. for (npages = 0; npages < pages_per_frame; npages++, index++) {
  795. struct folio *folio;
  796. folio = __filemap_get_folio(mapping, index,
  797. FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  798. gfp_mask);
  799. if (IS_ERR(folio)) {
  800. while (npages--) {
  801. folio = page_folio(pages[npages]);
  802. folio_unlock(folio);
  803. folio_put(folio);
  804. }
  805. return -ENOMEM;
  806. }
  807. if (!folio_test_uptodate(folio))
  808. *frame_uptodate = false;
  809. pages[npages] = &folio->page;
  810. }
  811. return 0;
  812. }
  813. /*
  814. * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
  815. */
  816. static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
  817. {
  818. int err;
  819. struct file *file = iocb->ki_filp;
  820. size_t count = iov_iter_count(from);
  821. loff_t pos = iocb->ki_pos;
  822. struct inode *inode = file_inode(file);
  823. loff_t i_size = i_size_read(inode);
  824. struct address_space *mapping = inode->i_mapping;
  825. struct ntfs_inode *ni = ntfs_i(inode);
  826. u64 valid = ni->i_valid;
  827. struct ntfs_sb_info *sbi = ni->mi.sbi;
  828. struct page *page, **pages = NULL;
  829. size_t written = 0;
  830. u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
  831. u32 frame_size = 1u << frame_bits;
  832. u32 pages_per_frame = frame_size >> PAGE_SHIFT;
  833. u32 ip, off;
  834. CLST frame;
  835. u64 frame_vbo;
  836. pgoff_t index;
  837. bool frame_uptodate;
  838. if (frame_size < PAGE_SIZE) {
  839. /*
  840. * frame_size == 8K if cluster 512
  841. * frame_size == 64K if cluster 4096
  842. */
  843. ntfs_inode_warn(inode, "page size is bigger than frame size");
  844. return -EOPNOTSUPP;
  845. }
  846. pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
  847. if (!pages)
  848. return -ENOMEM;
  849. err = file_remove_privs(file);
  850. if (err)
  851. goto out;
  852. err = file_update_time(file);
  853. if (err)
  854. goto out;
  855. /* Zero range [valid : pos). */
  856. while (valid < pos) {
  857. CLST lcn, clen;
  858. frame = valid >> frame_bits;
  859. frame_vbo = valid & ~(frame_size - 1);
  860. off = valid & (frame_size - 1);
  861. err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
  862. &clen, NULL, false);
  863. if (err)
  864. goto out;
  865. if (lcn == SPARSE_LCN) {
  866. ni->i_valid = valid =
  867. frame_vbo + ((u64)clen << sbi->cluster_bits);
  868. continue;
  869. }
  870. /* Load full frame. */
  871. err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
  872. pages, pages_per_frame,
  873. &frame_uptodate);
  874. if (err)
  875. goto out;
  876. if (!frame_uptodate && off) {
  877. err = ni_read_frame(ni, frame_vbo, pages,
  878. pages_per_frame);
  879. if (err) {
  880. for (ip = 0; ip < pages_per_frame; ip++) {
  881. page = pages[ip];
  882. unlock_page(page);
  883. put_page(page);
  884. }
  885. goto out;
  886. }
  887. }
  888. ip = off >> PAGE_SHIFT;
  889. off = offset_in_page(valid);
  890. for (; ip < pages_per_frame; ip++, off = 0) {
  891. page = pages[ip];
  892. zero_user_segment(page, off, PAGE_SIZE);
  893. flush_dcache_page(page);
  894. SetPageUptodate(page);
  895. }
  896. ni_lock(ni);
  897. err = ni_write_frame(ni, pages, pages_per_frame);
  898. ni_unlock(ni);
  899. for (ip = 0; ip < pages_per_frame; ip++) {
  900. page = pages[ip];
  901. SetPageUptodate(page);
  902. unlock_page(page);
  903. put_page(page);
  904. }
  905. if (err)
  906. goto out;
  907. ni->i_valid = valid = frame_vbo + frame_size;
  908. }
  909. /* Copy user data [pos : pos + count). */
  910. while (count) {
  911. size_t copied, bytes;
  912. off = pos & (frame_size - 1);
  913. bytes = frame_size - off;
  914. if (bytes > count)
  915. bytes = count;
  916. frame_vbo = pos & ~(frame_size - 1);
  917. index = frame_vbo >> PAGE_SHIFT;
  918. if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
  919. err = -EFAULT;
  920. goto out;
  921. }
  922. /* Load full frame. */
  923. err = ntfs_get_frame_pages(mapping, index, pages,
  924. pages_per_frame, &frame_uptodate);
  925. if (err)
  926. goto out;
  927. if (!frame_uptodate) {
  928. loff_t to = pos + bytes;
  929. if (off || (to < i_size && (to & (frame_size - 1)))) {
  930. err = ni_read_frame(ni, frame_vbo, pages,
  931. pages_per_frame);
  932. if (err) {
  933. for (ip = 0; ip < pages_per_frame;
  934. ip++) {
  935. page = pages[ip];
  936. unlock_page(page);
  937. put_page(page);
  938. }
  939. goto out;
  940. }
  941. }
  942. }
  943. WARN_ON(!bytes);
  944. copied = 0;
  945. ip = off >> PAGE_SHIFT;
  946. off = offset_in_page(pos);
  947. /* Copy user data to pages. */
  948. for (;;) {
  949. size_t cp, tail = PAGE_SIZE - off;
  950. page = pages[ip];
  951. cp = copy_page_from_iter_atomic(page, off,
  952. min(tail, bytes), from);
  953. flush_dcache_page(page);
  954. copied += cp;
  955. bytes -= cp;
  956. if (!bytes || !cp)
  957. break;
  958. if (cp < tail) {
  959. off += cp;
  960. } else {
  961. ip++;
  962. off = 0;
  963. }
  964. }
  965. ni_lock(ni);
  966. err = ni_write_frame(ni, pages, pages_per_frame);
  967. ni_unlock(ni);
  968. for (ip = 0; ip < pages_per_frame; ip++) {
  969. page = pages[ip];
  970. ClearPageDirty(page);
  971. SetPageUptodate(page);
  972. unlock_page(page);
  973. put_page(page);
  974. }
  975. if (err)
  976. goto out;
  977. /*
  978. * We can loop for a long time in here. Be nice and allow
  979. * us to schedule out to avoid softlocking if preempt
  980. * is disabled.
  981. */
  982. cond_resched();
  983. pos += copied;
  984. written += copied;
  985. count = iov_iter_count(from);
  986. }
  987. out:
  988. kfree(pages);
  989. if (err < 0)
  990. return err;
  991. iocb->ki_pos += written;
  992. if (iocb->ki_pos > ni->i_valid)
  993. ni->i_valid = iocb->ki_pos;
  994. if (iocb->ki_pos > i_size)
  995. i_size_write(inode, iocb->ki_pos);
  996. return written;
  997. }
  998. /*
  999. * check_write_restriction:
  1000. * common code for ntfs_file_write_iter and ntfs_file_splice_write
  1001. */
  1002. static int check_write_restriction(struct inode *inode)
  1003. {
  1004. struct ntfs_inode *ni = ntfs_i(inode);
  1005. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  1006. return -EIO;
  1007. if (is_encrypted(ni)) {
  1008. ntfs_inode_warn(inode, "encrypted i/o not supported");
  1009. return -EOPNOTSUPP;
  1010. }
  1011. if (is_dedup(ni)) {
  1012. ntfs_inode_warn(inode, "write into deduplicated not supported");
  1013. return -EOPNOTSUPP;
  1014. }
  1015. return 0;
  1016. }
  1017. /*
  1018. * ntfs_file_write_iter - file_operations::write_iter
  1019. */
  1020. static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1021. {
  1022. struct file *file = iocb->ki_filp;
  1023. struct inode *inode = file_inode(file);
  1024. struct ntfs_inode *ni = ntfs_i(inode);
  1025. ssize_t ret;
  1026. int err;
  1027. if (!inode_trylock(inode)) {
  1028. if (iocb->ki_flags & IOCB_NOWAIT)
  1029. return -EAGAIN;
  1030. inode_lock(inode);
  1031. }
  1032. ret = check_write_restriction(inode);
  1033. if (ret)
  1034. goto out;
  1035. if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
  1036. ntfs_inode_warn(inode, "direct i/o + compressed not supported");
  1037. ret = -EOPNOTSUPP;
  1038. goto out;
  1039. }
  1040. ret = generic_write_checks(iocb, from);
  1041. if (ret <= 0)
  1042. goto out;
  1043. err = file_modified(iocb->ki_filp);
  1044. if (err) {
  1045. ret = err;
  1046. goto out;
  1047. }
  1048. if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
  1049. /* Should never be here, see ntfs_file_open(). */
  1050. ret = -EOPNOTSUPP;
  1051. goto out;
  1052. }
  1053. ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
  1054. if (ret)
  1055. goto out;
  1056. ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) :
  1057. __generic_file_write_iter(iocb, from);
  1058. out:
  1059. inode_unlock(inode);
  1060. if (ret > 0)
  1061. ret = generic_write_sync(iocb, ret);
  1062. return ret;
  1063. }
  1064. /*
  1065. * ntfs_file_open - file_operations::open
  1066. */
  1067. int ntfs_file_open(struct inode *inode, struct file *file)
  1068. {
  1069. struct ntfs_inode *ni = ntfs_i(inode);
  1070. if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
  1071. return -EIO;
  1072. if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
  1073. (file->f_flags & O_DIRECT))) {
  1074. return -EOPNOTSUPP;
  1075. }
  1076. /* Decompress "external compressed" file if opened for rw. */
  1077. if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
  1078. (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
  1079. #ifdef CONFIG_NTFS3_LZX_XPRESS
  1080. int err = ni_decompress_file(ni);
  1081. if (err)
  1082. return err;
  1083. #else
  1084. ntfs_inode_warn(
  1085. inode,
  1086. "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
  1087. return -EOPNOTSUPP;
  1088. #endif
  1089. }
  1090. return generic_file_open(inode, file);
  1091. }
  1092. /*
  1093. * ntfs_file_release - file_operations::release
  1094. */
  1095. static int ntfs_file_release(struct inode *inode, struct file *file)
  1096. {
  1097. struct ntfs_inode *ni = ntfs_i(inode);
  1098. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1099. int err = 0;
  1100. /* If we are last writer on the inode, drop the block reservation. */
  1101. if (sbi->options->prealloc &&
  1102. ((file->f_mode & FMODE_WRITE) &&
  1103. atomic_read(&inode->i_writecount) == 1)
  1104. /*
  1105. * The only file when inode->i_fop = &ntfs_file_operations and
  1106. * init_rwsem(&ni->file.run_lock) is not called explicitly is MFT.
  1107. *
  1108. * Add additional check here.
  1109. */
  1110. && inode->i_ino != MFT_REC_MFT) {
  1111. ni_lock(ni);
  1112. down_write(&ni->file.run_lock);
  1113. err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
  1114. i_size_read(inode), &ni->i_valid, false,
  1115. NULL);
  1116. up_write(&ni->file.run_lock);
  1117. ni_unlock(ni);
  1118. }
  1119. return err;
  1120. }
  1121. /*
  1122. * ntfs_fiemap - inode_operations::fiemap
  1123. */
  1124. int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1125. __u64 start, __u64 len)
  1126. {
  1127. int err;
  1128. struct ntfs_inode *ni = ntfs_i(inode);
  1129. err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
  1130. if (err)
  1131. return err;
  1132. ni_lock(ni);
  1133. err = ni_fiemap(ni, fieinfo, start, len);
  1134. ni_unlock(ni);
  1135. return err;
  1136. }
  1137. /*
  1138. * ntfs_file_splice_write - file_operations::splice_write
  1139. */
  1140. static ssize_t ntfs_file_splice_write(struct pipe_inode_info *pipe,
  1141. struct file *file, loff_t *ppos,
  1142. size_t len, unsigned int flags)
  1143. {
  1144. ssize_t err;
  1145. struct inode *inode = file_inode(file);
  1146. err = check_write_restriction(inode);
  1147. if (err)
  1148. return err;
  1149. return iter_file_splice_write(pipe, file, ppos, len, flags);
  1150. }
  1151. // clang-format off
  1152. const struct inode_operations ntfs_file_inode_operations = {
  1153. .getattr = ntfs_getattr,
  1154. .setattr = ntfs_setattr,
  1155. .listxattr = ntfs_listxattr,
  1156. .get_acl = ntfs_get_acl,
  1157. .set_acl = ntfs_set_acl,
  1158. .fiemap = ntfs_fiemap,
  1159. .fileattr_get = ntfs_fileattr_get,
  1160. .fileattr_set = ntfs_fileattr_set,
  1161. };
  1162. const struct file_operations ntfs_file_operations = {
  1163. .llseek = generic_file_llseek,
  1164. .read_iter = ntfs_file_read_iter,
  1165. .write_iter = ntfs_file_write_iter,
  1166. .unlocked_ioctl = ntfs_ioctl,
  1167. #ifdef CONFIG_COMPAT
  1168. .compat_ioctl = ntfs_compat_ioctl,
  1169. #endif
  1170. .splice_read = ntfs_file_splice_read,
  1171. .splice_write = ntfs_file_splice_write,
  1172. .mmap = ntfs_file_mmap,
  1173. .open = ntfs_file_open,
  1174. .fsync = generic_file_fsync,
  1175. .fallocate = ntfs_fallocate,
  1176. .release = ntfs_file_release,
  1177. };
  1178. #if IS_ENABLED(CONFIG_NTFS_FS)
  1179. const struct file_operations ntfs_legacy_file_operations = {
  1180. .llseek = generic_file_llseek,
  1181. .read_iter = ntfs_file_read_iter,
  1182. .splice_read = ntfs_file_splice_read,
  1183. .open = ntfs_file_open,
  1184. .release = ntfs_file_release,
  1185. };
  1186. #endif
  1187. // clang-format on