file.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/compat.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/uio.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/mm.h>
  15. #include <linux/mount.h>
  16. #include <linux/fs.h>
  17. #include <linux/filelock.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/falloc.h>
  20. #include <linux/swap.h>
  21. #include <linux/crc32.h>
  22. #include <linux/writeback.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/dlm.h>
  25. #include <linux/dlm_plock.h>
  26. #include <linux/delay.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/fileattr.h>
  29. #include "gfs2.h"
  30. #include "incore.h"
  31. #include "bmap.h"
  32. #include "aops.h"
  33. #include "dir.h"
  34. #include "glock.h"
  35. #include "glops.h"
  36. #include "inode.h"
  37. #include "log.h"
  38. #include "meta_io.h"
  39. #include "quota.h"
  40. #include "rgrp.h"
  41. #include "trans.h"
  42. #include "util.h"
  43. /**
  44. * gfs2_llseek - seek to a location in a file
  45. * @file: the file
  46. * @offset: the offset
  47. * @whence: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  48. *
  49. * SEEK_END requires the glock for the file because it references the
  50. * file's size.
  51. *
  52. * Returns: The new offset, or errno
  53. */
  54. static loff_t gfs2_llseek(struct file *file, loff_t offset, int whence)
  55. {
  56. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  57. struct gfs2_holder i_gh;
  58. loff_t error;
  59. switch (whence) {
  60. case SEEK_END:
  61. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  62. &i_gh);
  63. if (!error) {
  64. error = generic_file_llseek(file, offset, whence);
  65. gfs2_glock_dq_uninit(&i_gh);
  66. }
  67. break;
  68. case SEEK_DATA:
  69. error = gfs2_seek_data(file, offset);
  70. break;
  71. case SEEK_HOLE:
  72. error = gfs2_seek_hole(file, offset);
  73. break;
  74. case SEEK_CUR:
  75. case SEEK_SET:
  76. /*
  77. * These don't reference inode->i_size and don't depend on the
  78. * block mapping, so we don't need the glock.
  79. */
  80. error = generic_file_llseek(file, offset, whence);
  81. break;
  82. default:
  83. error = -EINVAL;
  84. }
  85. return error;
  86. }
  87. /**
  88. * gfs2_readdir - Iterator for a directory
  89. * @file: The directory to read from
  90. * @ctx: What to feed directory entries to
  91. *
  92. * Returns: errno
  93. */
  94. static int gfs2_readdir(struct file *file, struct dir_context *ctx)
  95. {
  96. struct inode *dir = file->f_mapping->host;
  97. struct gfs2_inode *dip = GFS2_I(dir);
  98. struct gfs2_holder d_gh;
  99. int error;
  100. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  101. if (error)
  102. return error;
  103. error = gfs2_dir_read(dir, ctx, &file->f_ra);
  104. gfs2_glock_dq_uninit(&d_gh);
  105. return error;
  106. }
  107. /*
  108. * struct fsflag_gfs2flag
  109. *
  110. * The FS_JOURNAL_DATA_FL flag maps to GFS2_DIF_INHERIT_JDATA for directories,
  111. * and to GFS2_DIF_JDATA for non-directories.
  112. */
  113. static struct {
  114. u32 fsflag;
  115. u32 gfsflag;
  116. } fsflag_gfs2flag[] = {
  117. {FS_SYNC_FL, GFS2_DIF_SYNC},
  118. {FS_IMMUTABLE_FL, GFS2_DIF_IMMUTABLE},
  119. {FS_APPEND_FL, GFS2_DIF_APPENDONLY},
  120. {FS_NOATIME_FL, GFS2_DIF_NOATIME},
  121. {FS_INDEX_FL, GFS2_DIF_EXHASH},
  122. {FS_TOPDIR_FL, GFS2_DIF_TOPDIR},
  123. {FS_JOURNAL_DATA_FL, GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA},
  124. };
  125. static inline u32 gfs2_gfsflags_to_fsflags(struct inode *inode, u32 gfsflags)
  126. {
  127. int i;
  128. u32 fsflags = 0;
  129. if (S_ISDIR(inode->i_mode))
  130. gfsflags &= ~GFS2_DIF_JDATA;
  131. else
  132. gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
  133. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
  134. if (gfsflags & fsflag_gfs2flag[i].gfsflag)
  135. fsflags |= fsflag_gfs2flag[i].fsflag;
  136. return fsflags;
  137. }
  138. int gfs2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  139. {
  140. struct inode *inode = d_inode(dentry);
  141. struct gfs2_inode *ip = GFS2_I(inode);
  142. struct gfs2_holder gh;
  143. int error;
  144. u32 fsflags;
  145. if (d_is_special(dentry))
  146. return -ENOTTY;
  147. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  148. error = gfs2_glock_nq(&gh);
  149. if (error)
  150. goto out_uninit;
  151. fsflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
  152. fileattr_fill_flags(fa, fsflags);
  153. gfs2_glock_dq(&gh);
  154. out_uninit:
  155. gfs2_holder_uninit(&gh);
  156. return error;
  157. }
  158. void gfs2_set_inode_flags(struct inode *inode)
  159. {
  160. struct gfs2_inode *ip = GFS2_I(inode);
  161. unsigned int flags = inode->i_flags;
  162. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  163. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  164. flags |= S_NOSEC;
  165. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  166. flags |= S_IMMUTABLE;
  167. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  168. flags |= S_APPEND;
  169. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  170. flags |= S_NOATIME;
  171. if (ip->i_diskflags & GFS2_DIF_SYNC)
  172. flags |= S_SYNC;
  173. inode->i_flags = flags;
  174. }
  175. /* Flags that can be set by user space */
  176. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  177. GFS2_DIF_IMMUTABLE| \
  178. GFS2_DIF_APPENDONLY| \
  179. GFS2_DIF_NOATIME| \
  180. GFS2_DIF_SYNC| \
  181. GFS2_DIF_TOPDIR| \
  182. GFS2_DIF_INHERIT_JDATA)
  183. /**
  184. * do_gfs2_set_flags - set flags on an inode
  185. * @inode: The inode
  186. * @reqflags: The flags to set
  187. * @mask: Indicates which flags are valid
  188. *
  189. */
  190. static int do_gfs2_set_flags(struct inode *inode, u32 reqflags, u32 mask)
  191. {
  192. struct gfs2_inode *ip = GFS2_I(inode);
  193. struct gfs2_sbd *sdp = GFS2_SB(inode);
  194. struct buffer_head *bh;
  195. struct gfs2_holder gh;
  196. int error;
  197. u32 new_flags, flags;
  198. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  199. if (error)
  200. return error;
  201. error = 0;
  202. flags = ip->i_diskflags;
  203. new_flags = (flags & ~mask) | (reqflags & mask);
  204. if ((new_flags ^ flags) == 0)
  205. goto out;
  206. if (!IS_IMMUTABLE(inode)) {
  207. error = gfs2_permission(&nop_mnt_idmap, inode, MAY_WRITE);
  208. if (error)
  209. goto out;
  210. }
  211. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  212. if (new_flags & GFS2_DIF_JDATA)
  213. gfs2_log_flush(sdp, ip->i_gl,
  214. GFS2_LOG_HEAD_FLUSH_NORMAL |
  215. GFS2_LFC_SET_FLAGS);
  216. error = filemap_fdatawrite(inode->i_mapping);
  217. if (error)
  218. goto out;
  219. error = filemap_fdatawait(inode->i_mapping);
  220. if (error)
  221. goto out;
  222. truncate_inode_pages(inode->i_mapping, 0);
  223. if (new_flags & GFS2_DIF_JDATA)
  224. gfs2_ordered_del_inode(ip);
  225. }
  226. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  227. if (error)
  228. goto out;
  229. error = gfs2_meta_inode_buffer(ip, &bh);
  230. if (error)
  231. goto out_trans_end;
  232. inode_set_ctime_current(inode);
  233. gfs2_trans_add_meta(ip->i_gl, bh);
  234. ip->i_diskflags = new_flags;
  235. gfs2_dinode_out(ip, bh->b_data);
  236. brelse(bh);
  237. gfs2_set_inode_flags(inode);
  238. gfs2_set_aops(inode);
  239. out_trans_end:
  240. gfs2_trans_end(sdp);
  241. out:
  242. gfs2_glock_dq_uninit(&gh);
  243. return error;
  244. }
  245. int gfs2_fileattr_set(struct mnt_idmap *idmap,
  246. struct dentry *dentry, struct fileattr *fa)
  247. {
  248. struct inode *inode = d_inode(dentry);
  249. u32 fsflags = fa->flags, gfsflags = 0;
  250. u32 mask;
  251. int i;
  252. if (d_is_special(dentry))
  253. return -ENOTTY;
  254. if (fileattr_has_fsx(fa))
  255. return -EOPNOTSUPP;
  256. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
  257. if (fsflags & fsflag_gfs2flag[i].fsflag) {
  258. fsflags &= ~fsflag_gfs2flag[i].fsflag;
  259. gfsflags |= fsflag_gfs2flag[i].gfsflag;
  260. }
  261. }
  262. if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
  263. return -EINVAL;
  264. mask = GFS2_FLAGS_USER_SET;
  265. if (S_ISDIR(inode->i_mode)) {
  266. mask &= ~GFS2_DIF_JDATA;
  267. } else {
  268. /* The GFS2_DIF_TOPDIR flag is only valid for directories. */
  269. if (gfsflags & GFS2_DIF_TOPDIR)
  270. return -EINVAL;
  271. mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
  272. }
  273. return do_gfs2_set_flags(inode, gfsflags, mask);
  274. }
  275. static int gfs2_getlabel(struct file *filp, char __user *label)
  276. {
  277. struct inode *inode = file_inode(filp);
  278. struct gfs2_sbd *sdp = GFS2_SB(inode);
  279. if (copy_to_user(label, sdp->sd_sb.sb_locktable, GFS2_LOCKNAME_LEN))
  280. return -EFAULT;
  281. return 0;
  282. }
  283. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  284. {
  285. switch(cmd) {
  286. case FITRIM:
  287. return gfs2_fitrim(filp, (void __user *)arg);
  288. case FS_IOC_GETFSLABEL:
  289. return gfs2_getlabel(filp, (char __user *)arg);
  290. }
  291. return -ENOTTY;
  292. }
  293. #ifdef CONFIG_COMPAT
  294. static long gfs2_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  295. {
  296. switch(cmd) {
  297. /* Keep this list in sync with gfs2_ioctl */
  298. case FITRIM:
  299. case FS_IOC_GETFSLABEL:
  300. break;
  301. default:
  302. return -ENOIOCTLCMD;
  303. }
  304. return gfs2_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  305. }
  306. #else
  307. #define gfs2_compat_ioctl NULL
  308. #endif
  309. /**
  310. * gfs2_size_hint - Give a hint to the size of a write request
  311. * @filep: The struct file
  312. * @offset: The file offset of the write
  313. * @size: The length of the write
  314. *
  315. * When we are about to do a write, this function records the total
  316. * write size in order to provide a suitable hint to the lower layers
  317. * about how many blocks will be required.
  318. *
  319. */
  320. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  321. {
  322. struct inode *inode = file_inode(filep);
  323. struct gfs2_sbd *sdp = GFS2_SB(inode);
  324. struct gfs2_inode *ip = GFS2_I(inode);
  325. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  326. int hint = min_t(size_t, INT_MAX, blks);
  327. if (hint > atomic_read(&ip->i_sizehint))
  328. atomic_set(&ip->i_sizehint, hint);
  329. }
  330. /**
  331. * gfs2_allocate_folio_backing - Allocate blocks for a write fault
  332. * @folio: The (locked) folio to allocate backing for
  333. * @length: Size of the allocation
  334. *
  335. * We try to allocate all the blocks required for the folio in one go. This
  336. * might fail for various reasons, so we keep trying until all the blocks to
  337. * back this folio are allocated. If some of the blocks are already allocated,
  338. * that is ok too.
  339. */
  340. static int gfs2_allocate_folio_backing(struct folio *folio, size_t length)
  341. {
  342. u64 pos = folio_pos(folio);
  343. do {
  344. struct iomap iomap = { };
  345. if (gfs2_iomap_alloc(folio->mapping->host, pos, length, &iomap))
  346. return -EIO;
  347. if (length < iomap.length)
  348. iomap.length = length;
  349. length -= iomap.length;
  350. pos += iomap.length;
  351. } while (length > 0);
  352. return 0;
  353. }
  354. /**
  355. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  356. * @vmf: The virtual memory fault containing the page to become writable
  357. *
  358. * When the page becomes writable, we need to ensure that we have
  359. * blocks allocated on disk to back that page.
  360. */
  361. static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
  362. {
  363. struct folio *folio = page_folio(vmf->page);
  364. struct inode *inode = file_inode(vmf->vma->vm_file);
  365. struct gfs2_inode *ip = GFS2_I(inode);
  366. struct gfs2_sbd *sdp = GFS2_SB(inode);
  367. struct gfs2_alloc_parms ap = {};
  368. u64 pos = folio_pos(folio);
  369. unsigned int data_blocks, ind_blocks, rblocks;
  370. vm_fault_t ret = VM_FAULT_LOCKED;
  371. struct gfs2_holder gh;
  372. size_t length;
  373. loff_t size;
  374. int err;
  375. sb_start_pagefault(inode->i_sb);
  376. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  377. err = gfs2_glock_nq(&gh);
  378. if (err) {
  379. ret = vmf_fs_error(err);
  380. goto out_uninit;
  381. }
  382. /* Check folio index against inode size */
  383. size = i_size_read(inode);
  384. if (pos >= size) {
  385. ret = VM_FAULT_SIGBUS;
  386. goto out_unlock;
  387. }
  388. /* Update file times before taking folio lock */
  389. file_update_time(vmf->vma->vm_file);
  390. /* folio is wholly or partially inside EOF */
  391. if (size - pos < folio_size(folio))
  392. length = size - pos;
  393. else
  394. length = folio_size(folio);
  395. gfs2_size_hint(vmf->vma->vm_file, pos, length);
  396. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  397. set_bit(GIF_SW_PAGED, &ip->i_flags);
  398. /*
  399. * iomap_writepage / iomap_writepages currently don't support inline
  400. * files, so always unstuff here.
  401. */
  402. if (!gfs2_is_stuffed(ip) &&
  403. !gfs2_write_alloc_required(ip, pos, length)) {
  404. folio_lock(folio);
  405. if (!folio_test_uptodate(folio) ||
  406. folio->mapping != inode->i_mapping) {
  407. ret = VM_FAULT_NOPAGE;
  408. folio_unlock(folio);
  409. }
  410. goto out_unlock;
  411. }
  412. err = gfs2_rindex_update(sdp);
  413. if (err) {
  414. ret = vmf_fs_error(err);
  415. goto out_unlock;
  416. }
  417. gfs2_write_calc_reserv(ip, length, &data_blocks, &ind_blocks);
  418. ap.target = data_blocks + ind_blocks;
  419. err = gfs2_quota_lock_check(ip, &ap);
  420. if (err) {
  421. ret = vmf_fs_error(err);
  422. goto out_unlock;
  423. }
  424. err = gfs2_inplace_reserve(ip, &ap);
  425. if (err) {
  426. ret = vmf_fs_error(err);
  427. goto out_quota_unlock;
  428. }
  429. rblocks = RES_DINODE + ind_blocks;
  430. if (gfs2_is_jdata(ip))
  431. rblocks += data_blocks ? data_blocks : 1;
  432. if (ind_blocks || data_blocks) {
  433. rblocks += RES_STATFS + RES_QUOTA;
  434. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  435. }
  436. err = gfs2_trans_begin(sdp, rblocks, 0);
  437. if (err) {
  438. ret = vmf_fs_error(err);
  439. goto out_trans_fail;
  440. }
  441. /* Unstuff, if required, and allocate backing blocks for folio */
  442. if (gfs2_is_stuffed(ip)) {
  443. err = gfs2_unstuff_dinode(ip);
  444. if (err) {
  445. ret = vmf_fs_error(err);
  446. goto out_trans_end;
  447. }
  448. }
  449. folio_lock(folio);
  450. /* If truncated, we must retry the operation, we may have raced
  451. * with the glock demotion code.
  452. */
  453. if (!folio_test_uptodate(folio) || folio->mapping != inode->i_mapping) {
  454. ret = VM_FAULT_NOPAGE;
  455. goto out_page_locked;
  456. }
  457. err = gfs2_allocate_folio_backing(folio, length);
  458. if (err)
  459. ret = vmf_fs_error(err);
  460. out_page_locked:
  461. if (ret != VM_FAULT_LOCKED)
  462. folio_unlock(folio);
  463. out_trans_end:
  464. gfs2_trans_end(sdp);
  465. out_trans_fail:
  466. gfs2_inplace_release(ip);
  467. out_quota_unlock:
  468. gfs2_quota_unlock(ip);
  469. out_unlock:
  470. gfs2_glock_dq(&gh);
  471. out_uninit:
  472. gfs2_holder_uninit(&gh);
  473. if (ret == VM_FAULT_LOCKED) {
  474. folio_mark_dirty(folio);
  475. folio_wait_stable(folio);
  476. }
  477. sb_end_pagefault(inode->i_sb);
  478. return ret;
  479. }
  480. static vm_fault_t gfs2_fault(struct vm_fault *vmf)
  481. {
  482. struct inode *inode = file_inode(vmf->vma->vm_file);
  483. struct gfs2_inode *ip = GFS2_I(inode);
  484. struct gfs2_holder gh;
  485. vm_fault_t ret;
  486. int err;
  487. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  488. err = gfs2_glock_nq(&gh);
  489. if (err) {
  490. ret = vmf_fs_error(err);
  491. goto out_uninit;
  492. }
  493. ret = filemap_fault(vmf);
  494. gfs2_glock_dq(&gh);
  495. out_uninit:
  496. gfs2_holder_uninit(&gh);
  497. return ret;
  498. }
  499. static const struct vm_operations_struct gfs2_vm_ops = {
  500. .fault = gfs2_fault,
  501. .map_pages = filemap_map_pages,
  502. .page_mkwrite = gfs2_page_mkwrite,
  503. };
  504. /**
  505. * gfs2_mmap
  506. * @file: The file to map
  507. * @vma: The VMA which described the mapping
  508. *
  509. * There is no need to get a lock here unless we should be updating
  510. * atime. We ignore any locking errors since the only consequence is
  511. * a missed atime update (which will just be deferred until later).
  512. *
  513. * Returns: 0
  514. */
  515. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  516. {
  517. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  518. if (!(file->f_flags & O_NOATIME) &&
  519. !IS_NOATIME(&ip->i_inode)) {
  520. struct gfs2_holder i_gh;
  521. int error;
  522. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  523. &i_gh);
  524. if (error)
  525. return error;
  526. /* grab lock to update inode */
  527. gfs2_glock_dq_uninit(&i_gh);
  528. file_accessed(file);
  529. }
  530. vma->vm_ops = &gfs2_vm_ops;
  531. return 0;
  532. }
  533. /**
  534. * gfs2_open_common - This is common to open and atomic_open
  535. * @inode: The inode being opened
  536. * @file: The file being opened
  537. *
  538. * This maybe called under a glock or not depending upon how it has
  539. * been called. We must always be called under a glock for regular
  540. * files, however. For other file types, it does not matter whether
  541. * we hold the glock or not.
  542. *
  543. * Returns: Error code or 0 for success
  544. */
  545. int gfs2_open_common(struct inode *inode, struct file *file)
  546. {
  547. struct gfs2_file *fp;
  548. int ret;
  549. if (S_ISREG(inode->i_mode)) {
  550. ret = generic_file_open(inode, file);
  551. if (ret)
  552. return ret;
  553. if (!gfs2_is_jdata(GFS2_I(inode)))
  554. file->f_mode |= FMODE_CAN_ODIRECT;
  555. }
  556. fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
  557. if (!fp)
  558. return -ENOMEM;
  559. mutex_init(&fp->f_fl_mutex);
  560. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  561. file->private_data = fp;
  562. if (file->f_mode & FMODE_WRITE) {
  563. ret = gfs2_qa_get(GFS2_I(inode));
  564. if (ret)
  565. goto fail;
  566. }
  567. return 0;
  568. fail:
  569. kfree(file->private_data);
  570. file->private_data = NULL;
  571. return ret;
  572. }
  573. /**
  574. * gfs2_open - open a file
  575. * @inode: the inode to open
  576. * @file: the struct file for this opening
  577. *
  578. * After atomic_open, this function is only used for opening files
  579. * which are already cached. We must still get the glock for regular
  580. * files to ensure that we have the file size uptodate for the large
  581. * file check which is in the common code. That is only an issue for
  582. * regular files though.
  583. *
  584. * Returns: errno
  585. */
  586. static int gfs2_open(struct inode *inode, struct file *file)
  587. {
  588. struct gfs2_inode *ip = GFS2_I(inode);
  589. struct gfs2_holder i_gh;
  590. int error;
  591. bool need_unlock = false;
  592. if (S_ISREG(ip->i_inode.i_mode)) {
  593. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  594. &i_gh);
  595. if (error)
  596. return error;
  597. need_unlock = true;
  598. }
  599. error = gfs2_open_common(inode, file);
  600. if (need_unlock)
  601. gfs2_glock_dq_uninit(&i_gh);
  602. return error;
  603. }
  604. /**
  605. * gfs2_release - called to close a struct file
  606. * @inode: the inode the struct file belongs to
  607. * @file: the struct file being closed
  608. *
  609. * Returns: errno
  610. */
  611. static int gfs2_release(struct inode *inode, struct file *file)
  612. {
  613. struct gfs2_inode *ip = GFS2_I(inode);
  614. kfree(file->private_data);
  615. file->private_data = NULL;
  616. if (file->f_mode & FMODE_WRITE) {
  617. if (gfs2_rs_active(&ip->i_res))
  618. gfs2_rs_delete(ip);
  619. gfs2_qa_put(ip);
  620. }
  621. return 0;
  622. }
  623. /**
  624. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  625. * @file: the file that points to the dentry
  626. * @start: the start position in the file to sync
  627. * @end: the end position in the file to sync
  628. * @datasync: set if we can ignore timestamp changes
  629. *
  630. * We split the data flushing here so that we don't wait for the data
  631. * until after we've also sent the metadata to disk. Note that for
  632. * data=ordered, we will write & wait for the data at the log flush
  633. * stage anyway, so this is unlikely to make much of a difference
  634. * except in the data=writeback case.
  635. *
  636. * If the fdatawrite fails due to any reason except -EIO, we will
  637. * continue the remainder of the fsync, although we'll still report
  638. * the error at the end. This is to match filemap_write_and_wait_range()
  639. * behaviour.
  640. *
  641. * Returns: errno
  642. */
  643. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  644. int datasync)
  645. {
  646. struct address_space *mapping = file->f_mapping;
  647. struct inode *inode = mapping->host;
  648. int sync_state = inode->i_state & I_DIRTY;
  649. struct gfs2_inode *ip = GFS2_I(inode);
  650. int ret = 0, ret1 = 0;
  651. if (mapping->nrpages) {
  652. ret1 = filemap_fdatawrite_range(mapping, start, end);
  653. if (ret1 == -EIO)
  654. return ret1;
  655. }
  656. if (!gfs2_is_jdata(ip))
  657. sync_state &= ~I_DIRTY_PAGES;
  658. if (datasync)
  659. sync_state &= ~I_DIRTY_SYNC;
  660. if (sync_state) {
  661. ret = sync_inode_metadata(inode, 1);
  662. if (ret)
  663. return ret;
  664. if (gfs2_is_jdata(ip))
  665. ret = file_write_and_wait(file);
  666. if (ret)
  667. return ret;
  668. gfs2_ail_flush(ip->i_gl, 1);
  669. }
  670. if (mapping->nrpages)
  671. ret = file_fdatawait_range(file, start, end);
  672. return ret ? ret : ret1;
  673. }
  674. static inline bool should_fault_in_pages(struct iov_iter *i,
  675. struct kiocb *iocb,
  676. size_t *prev_count,
  677. size_t *window_size)
  678. {
  679. size_t count = iov_iter_count(i);
  680. size_t size, offs;
  681. if (!count)
  682. return false;
  683. if (!user_backed_iter(i))
  684. return false;
  685. /*
  686. * Try to fault in multiple pages initially. When that doesn't result
  687. * in any progress, fall back to a single page.
  688. */
  689. size = PAGE_SIZE;
  690. offs = offset_in_page(iocb->ki_pos);
  691. if (*prev_count != count) {
  692. size_t nr_dirtied;
  693. nr_dirtied = max(current->nr_dirtied_pause -
  694. current->nr_dirtied, 8);
  695. size = min_t(size_t, SZ_1M, nr_dirtied << PAGE_SHIFT);
  696. }
  697. *prev_count = count;
  698. *window_size = size - offs;
  699. return true;
  700. }
  701. static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
  702. struct gfs2_holder *gh)
  703. {
  704. struct file *file = iocb->ki_filp;
  705. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  706. size_t prev_count = 0, window_size = 0;
  707. size_t read = 0;
  708. ssize_t ret;
  709. /*
  710. * In this function, we disable page faults when we're holding the
  711. * inode glock while doing I/O. If a page fault occurs, we indicate
  712. * that the inode glock may be dropped, fault in the pages manually,
  713. * and retry.
  714. *
  715. * Unlike generic_file_read_iter, for reads, iomap_dio_rw can trigger
  716. * physical as well as manual page faults, and we need to disable both
  717. * kinds.
  718. *
  719. * For direct I/O, gfs2 takes the inode glock in deferred mode. This
  720. * locking mode is compatible with other deferred holders, so multiple
  721. * processes and nodes can do direct I/O to a file at the same time.
  722. * There's no guarantee that reads or writes will be atomic. Any
  723. * coordination among readers and writers needs to happen externally.
  724. */
  725. if (!iov_iter_count(to))
  726. return 0; /* skip atime */
  727. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
  728. retry:
  729. ret = gfs2_glock_nq(gh);
  730. if (ret)
  731. goto out_uninit;
  732. pagefault_disable();
  733. to->nofault = true;
  734. ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL,
  735. IOMAP_DIO_PARTIAL, NULL, read);
  736. to->nofault = false;
  737. pagefault_enable();
  738. if (ret <= 0 && ret != -EFAULT)
  739. goto out_unlock;
  740. /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
  741. if (ret > 0)
  742. read = ret;
  743. if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
  744. gfs2_glock_dq(gh);
  745. window_size -= fault_in_iov_iter_writeable(to, window_size);
  746. if (window_size)
  747. goto retry;
  748. }
  749. out_unlock:
  750. if (gfs2_holder_queued(gh))
  751. gfs2_glock_dq(gh);
  752. out_uninit:
  753. gfs2_holder_uninit(gh);
  754. /* User space doesn't expect partial success. */
  755. if (ret < 0)
  756. return ret;
  757. return read;
  758. }
  759. static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
  760. struct gfs2_holder *gh)
  761. {
  762. struct file *file = iocb->ki_filp;
  763. struct inode *inode = file->f_mapping->host;
  764. struct gfs2_inode *ip = GFS2_I(inode);
  765. size_t prev_count = 0, window_size = 0;
  766. size_t written = 0;
  767. bool enough_retries;
  768. ssize_t ret;
  769. /*
  770. * In this function, we disable page faults when we're holding the
  771. * inode glock while doing I/O. If a page fault occurs, we indicate
  772. * that the inode glock may be dropped, fault in the pages manually,
  773. * and retry.
  774. *
  775. * For writes, iomap_dio_rw only triggers manual page faults, so we
  776. * don't need to disable physical ones.
  777. */
  778. /*
  779. * Deferred lock, even if its a write, since we do no allocation on
  780. * this path. All we need to change is the atime, and this lock mode
  781. * ensures that other nodes have flushed their buffered read caches
  782. * (i.e. their page cache entries for this inode). We do not,
  783. * unfortunately, have the option of only flushing a range like the
  784. * VFS does.
  785. */
  786. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
  787. retry:
  788. ret = gfs2_glock_nq(gh);
  789. if (ret)
  790. goto out_uninit;
  791. /* Silently fall back to buffered I/O when writing beyond EOF */
  792. if (iocb->ki_pos + iov_iter_count(from) > i_size_read(&ip->i_inode))
  793. goto out_unlock;
  794. from->nofault = true;
  795. ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
  796. IOMAP_DIO_PARTIAL, NULL, written);
  797. from->nofault = false;
  798. if (ret <= 0) {
  799. if (ret == -ENOTBLK)
  800. ret = 0;
  801. if (ret != -EFAULT)
  802. goto out_unlock;
  803. }
  804. /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
  805. if (ret > 0)
  806. written = ret;
  807. enough_retries = prev_count == iov_iter_count(from) &&
  808. window_size <= PAGE_SIZE;
  809. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  810. gfs2_glock_dq(gh);
  811. window_size -= fault_in_iov_iter_readable(from, window_size);
  812. if (window_size) {
  813. if (!enough_retries)
  814. goto retry;
  815. /* fall back to buffered I/O */
  816. ret = 0;
  817. }
  818. }
  819. out_unlock:
  820. if (gfs2_holder_queued(gh))
  821. gfs2_glock_dq(gh);
  822. out_uninit:
  823. gfs2_holder_uninit(gh);
  824. /* User space doesn't expect partial success. */
  825. if (ret < 0)
  826. return ret;
  827. return written;
  828. }
  829. static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  830. {
  831. struct gfs2_inode *ip;
  832. struct gfs2_holder gh;
  833. size_t prev_count = 0, window_size = 0;
  834. size_t read = 0;
  835. ssize_t ret;
  836. /*
  837. * In this function, we disable page faults when we're holding the
  838. * inode glock while doing I/O. If a page fault occurs, we indicate
  839. * that the inode glock may be dropped, fault in the pages manually,
  840. * and retry.
  841. */
  842. if (iocb->ki_flags & IOCB_DIRECT)
  843. return gfs2_file_direct_read(iocb, to, &gh);
  844. pagefault_disable();
  845. iocb->ki_flags |= IOCB_NOIO;
  846. ret = generic_file_read_iter(iocb, to);
  847. iocb->ki_flags &= ~IOCB_NOIO;
  848. pagefault_enable();
  849. if (ret >= 0) {
  850. if (!iov_iter_count(to))
  851. return ret;
  852. read = ret;
  853. } else if (ret != -EFAULT) {
  854. if (ret != -EAGAIN)
  855. return ret;
  856. if (iocb->ki_flags & IOCB_NOWAIT)
  857. return ret;
  858. }
  859. ip = GFS2_I(iocb->ki_filp->f_mapping->host);
  860. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  861. retry:
  862. ret = gfs2_glock_nq(&gh);
  863. if (ret)
  864. goto out_uninit;
  865. pagefault_disable();
  866. ret = generic_file_read_iter(iocb, to);
  867. pagefault_enable();
  868. if (ret <= 0 && ret != -EFAULT)
  869. goto out_unlock;
  870. if (ret > 0)
  871. read += ret;
  872. if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
  873. gfs2_glock_dq(&gh);
  874. window_size -= fault_in_iov_iter_writeable(to, window_size);
  875. if (window_size)
  876. goto retry;
  877. }
  878. out_unlock:
  879. if (gfs2_holder_queued(&gh))
  880. gfs2_glock_dq(&gh);
  881. out_uninit:
  882. gfs2_holder_uninit(&gh);
  883. return read ? read : ret;
  884. }
  885. static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
  886. struct iov_iter *from,
  887. struct gfs2_holder *gh)
  888. {
  889. struct file *file = iocb->ki_filp;
  890. struct inode *inode = file_inode(file);
  891. struct gfs2_inode *ip = GFS2_I(inode);
  892. struct gfs2_sbd *sdp = GFS2_SB(inode);
  893. struct gfs2_holder *statfs_gh = NULL;
  894. size_t prev_count = 0, window_size = 0;
  895. size_t orig_count = iov_iter_count(from);
  896. size_t written = 0;
  897. ssize_t ret;
  898. /*
  899. * In this function, we disable page faults when we're holding the
  900. * inode glock while doing I/O. If a page fault occurs, we indicate
  901. * that the inode glock may be dropped, fault in the pages manually,
  902. * and retry.
  903. */
  904. if (inode == sdp->sd_rindex) {
  905. statfs_gh = kmalloc(sizeof(*statfs_gh), GFP_NOFS);
  906. if (!statfs_gh)
  907. return -ENOMEM;
  908. }
  909. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, gh);
  910. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  911. retry:
  912. window_size -= fault_in_iov_iter_readable(from, window_size);
  913. if (!window_size) {
  914. ret = -EFAULT;
  915. goto out_uninit;
  916. }
  917. from->count = min(from->count, window_size);
  918. }
  919. ret = gfs2_glock_nq(gh);
  920. if (ret)
  921. goto out_uninit;
  922. if (inode == sdp->sd_rindex) {
  923. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  924. ret = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
  925. GL_NOCACHE, statfs_gh);
  926. if (ret)
  927. goto out_unlock;
  928. }
  929. pagefault_disable();
  930. ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops, NULL);
  931. pagefault_enable();
  932. if (ret > 0)
  933. written += ret;
  934. if (inode == sdp->sd_rindex)
  935. gfs2_glock_dq_uninit(statfs_gh);
  936. if (ret <= 0 && ret != -EFAULT)
  937. goto out_unlock;
  938. from->count = orig_count - written;
  939. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  940. gfs2_glock_dq(gh);
  941. goto retry;
  942. }
  943. out_unlock:
  944. if (gfs2_holder_queued(gh))
  945. gfs2_glock_dq(gh);
  946. out_uninit:
  947. gfs2_holder_uninit(gh);
  948. kfree(statfs_gh);
  949. from->count = orig_count - written;
  950. return written ? written : ret;
  951. }
  952. /**
  953. * gfs2_file_write_iter - Perform a write to a file
  954. * @iocb: The io context
  955. * @from: The data to write
  956. *
  957. * We have to do a lock/unlock here to refresh the inode size for
  958. * O_APPEND writes, otherwise we can land up writing at the wrong
  959. * offset. There is still a race, but provided the app is using its
  960. * own file locking, this will make O_APPEND work as expected.
  961. *
  962. */
  963. static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  964. {
  965. struct file *file = iocb->ki_filp;
  966. struct inode *inode = file_inode(file);
  967. struct gfs2_inode *ip = GFS2_I(inode);
  968. struct gfs2_holder gh;
  969. ssize_t ret;
  970. gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
  971. if (iocb->ki_flags & IOCB_APPEND) {
  972. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  973. if (ret)
  974. return ret;
  975. gfs2_glock_dq_uninit(&gh);
  976. }
  977. inode_lock(inode);
  978. ret = generic_write_checks(iocb, from);
  979. if (ret <= 0)
  980. goto out_unlock;
  981. ret = file_remove_privs(file);
  982. if (ret)
  983. goto out_unlock;
  984. if (iocb->ki_flags & IOCB_DIRECT) {
  985. struct address_space *mapping = file->f_mapping;
  986. ssize_t buffered, ret2;
  987. /*
  988. * Note that under direct I/O, we don't allow and inode
  989. * timestamp updates, so we're not calling file_update_time()
  990. * here.
  991. */
  992. ret = gfs2_file_direct_write(iocb, from, &gh);
  993. if (ret < 0 || !iov_iter_count(from))
  994. goto out_unlock;
  995. iocb->ki_flags |= IOCB_DSYNC;
  996. buffered = gfs2_file_buffered_write(iocb, from, &gh);
  997. if (unlikely(buffered <= 0)) {
  998. if (!ret)
  999. ret = buffered;
  1000. goto out_unlock;
  1001. }
  1002. /*
  1003. * We need to ensure that the page cache pages are written to
  1004. * disk and invalidated to preserve the expected O_DIRECT
  1005. * semantics. If the writeback or invalidate fails, only report
  1006. * the direct I/O range as we don't know if the buffered pages
  1007. * made it to disk.
  1008. */
  1009. ret2 = generic_write_sync(iocb, buffered);
  1010. invalidate_mapping_pages(mapping,
  1011. (iocb->ki_pos - buffered) >> PAGE_SHIFT,
  1012. (iocb->ki_pos - 1) >> PAGE_SHIFT);
  1013. if (!ret || ret2 > 0)
  1014. ret += ret2;
  1015. } else {
  1016. ret = file_update_time(file);
  1017. if (ret)
  1018. goto out_unlock;
  1019. ret = gfs2_file_buffered_write(iocb, from, &gh);
  1020. if (likely(ret > 0))
  1021. ret = generic_write_sync(iocb, ret);
  1022. }
  1023. out_unlock:
  1024. inode_unlock(inode);
  1025. return ret;
  1026. }
  1027. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1028. int mode)
  1029. {
  1030. struct super_block *sb = inode->i_sb;
  1031. struct gfs2_inode *ip = GFS2_I(inode);
  1032. loff_t end = offset + len;
  1033. struct buffer_head *dibh;
  1034. int error;
  1035. error = gfs2_meta_inode_buffer(ip, &dibh);
  1036. if (unlikely(error))
  1037. return error;
  1038. gfs2_trans_add_meta(ip->i_gl, dibh);
  1039. if (gfs2_is_stuffed(ip)) {
  1040. error = gfs2_unstuff_dinode(ip);
  1041. if (unlikely(error))
  1042. goto out;
  1043. }
  1044. while (offset < end) {
  1045. struct iomap iomap = { };
  1046. error = gfs2_iomap_alloc(inode, offset, end - offset, &iomap);
  1047. if (error)
  1048. goto out;
  1049. offset = iomap.offset + iomap.length;
  1050. if (!(iomap.flags & IOMAP_F_NEW))
  1051. continue;
  1052. error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
  1053. iomap.length >> inode->i_blkbits,
  1054. GFP_NOFS);
  1055. if (error) {
  1056. fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
  1057. goto out;
  1058. }
  1059. }
  1060. out:
  1061. brelse(dibh);
  1062. return error;
  1063. }
  1064. /**
  1065. * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
  1066. * blocks, determine how many bytes can be written.
  1067. * @ip: The inode in question.
  1068. * @len: Max cap of bytes. What we return in *len must be <= this.
  1069. * @data_blocks: Compute and return the number of data blocks needed
  1070. * @ind_blocks: Compute and return the number of indirect blocks needed
  1071. * @max_blocks: The total blocks available to work with.
  1072. *
  1073. * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
  1074. */
  1075. static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
  1076. unsigned int *data_blocks, unsigned int *ind_blocks,
  1077. unsigned int max_blocks)
  1078. {
  1079. loff_t max = *len;
  1080. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1081. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1082. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1083. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1084. max_data -= tmp;
  1085. }
  1086. *data_blocks = max_data;
  1087. *ind_blocks = max_blocks - max_data;
  1088. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1089. if (*len > max) {
  1090. *len = max;
  1091. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1092. }
  1093. }
  1094. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  1095. {
  1096. struct inode *inode = file_inode(file);
  1097. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1098. struct gfs2_inode *ip = GFS2_I(inode);
  1099. struct gfs2_alloc_parms ap = {};
  1100. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1101. loff_t bytes, max_bytes, max_blks;
  1102. int error;
  1103. const loff_t pos = offset;
  1104. const loff_t count = len;
  1105. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  1106. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1107. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  1108. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1109. offset &= bsize_mask;
  1110. len = next - offset;
  1111. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1112. if (!bytes)
  1113. bytes = UINT_MAX;
  1114. bytes &= bsize_mask;
  1115. if (bytes == 0)
  1116. bytes = sdp->sd_sb.sb_bsize;
  1117. gfs2_size_hint(file, offset, len);
  1118. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  1119. ap.min_target = data_blocks + ind_blocks;
  1120. while (len > 0) {
  1121. if (len < bytes)
  1122. bytes = len;
  1123. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  1124. len -= bytes;
  1125. offset += bytes;
  1126. continue;
  1127. }
  1128. /* We need to determine how many bytes we can actually
  1129. * fallocate without exceeding quota or going over the
  1130. * end of the fs. We start off optimistically by assuming
  1131. * we can write max_bytes */
  1132. max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
  1133. /* Since max_bytes is most likely a theoretical max, we
  1134. * calculate a more realistic 'bytes' to serve as a good
  1135. * starting point for the number of bytes we may be able
  1136. * to write */
  1137. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1138. ap.target = data_blocks + ind_blocks;
  1139. error = gfs2_quota_lock_check(ip, &ap);
  1140. if (error)
  1141. return error;
  1142. /* ap.allowed tells us how many blocks quota will allow
  1143. * us to write. Check if this reduces max_blks */
  1144. max_blks = UINT_MAX;
  1145. if (ap.allowed)
  1146. max_blks = ap.allowed;
  1147. error = gfs2_inplace_reserve(ip, &ap);
  1148. if (error)
  1149. goto out_qunlock;
  1150. /* check if the selected rgrp limits our max_blks further */
  1151. if (ip->i_res.rs_reserved < max_blks)
  1152. max_blks = ip->i_res.rs_reserved;
  1153. /* Almost done. Calculate bytes that can be written using
  1154. * max_blks. We also recompute max_bytes, data_blocks and
  1155. * ind_blocks */
  1156. calc_max_reserv(ip, &max_bytes, &data_blocks,
  1157. &ind_blocks, max_blks);
  1158. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1159. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  1160. if (gfs2_is_jdata(ip))
  1161. rblocks += data_blocks ? data_blocks : 1;
  1162. error = gfs2_trans_begin(sdp, rblocks,
  1163. PAGE_SIZE >> inode->i_blkbits);
  1164. if (error)
  1165. goto out_trans_fail;
  1166. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1167. gfs2_trans_end(sdp);
  1168. if (error)
  1169. goto out_trans_fail;
  1170. len -= max_bytes;
  1171. offset += max_bytes;
  1172. gfs2_inplace_release(ip);
  1173. gfs2_quota_unlock(ip);
  1174. }
  1175. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size)
  1176. i_size_write(inode, pos + count);
  1177. file_update_time(file);
  1178. mark_inode_dirty(inode);
  1179. if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
  1180. return vfs_fsync_range(file, pos, pos + count - 1,
  1181. (file->f_flags & __O_SYNC) ? 0 : 1);
  1182. return 0;
  1183. out_trans_fail:
  1184. gfs2_inplace_release(ip);
  1185. out_qunlock:
  1186. gfs2_quota_unlock(ip);
  1187. return error;
  1188. }
  1189. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  1190. {
  1191. struct inode *inode = file_inode(file);
  1192. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1193. struct gfs2_inode *ip = GFS2_I(inode);
  1194. struct gfs2_holder gh;
  1195. int ret;
  1196. if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
  1197. return -EOPNOTSUPP;
  1198. /* fallocate is needed by gfs2_grow to reserve space in the rindex */
  1199. if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
  1200. return -EOPNOTSUPP;
  1201. inode_lock(inode);
  1202. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1203. ret = gfs2_glock_nq(&gh);
  1204. if (ret)
  1205. goto out_uninit;
  1206. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  1207. (offset + len) > inode->i_size) {
  1208. ret = inode_newsize_ok(inode, offset + len);
  1209. if (ret)
  1210. goto out_unlock;
  1211. }
  1212. ret = get_write_access(inode);
  1213. if (ret)
  1214. goto out_unlock;
  1215. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1216. ret = __gfs2_punch_hole(file, offset, len);
  1217. } else {
  1218. ret = __gfs2_fallocate(file, mode, offset, len);
  1219. if (ret)
  1220. gfs2_rs_deltree(&ip->i_res);
  1221. }
  1222. put_write_access(inode);
  1223. out_unlock:
  1224. gfs2_glock_dq(&gh);
  1225. out_uninit:
  1226. gfs2_holder_uninit(&gh);
  1227. inode_unlock(inode);
  1228. return ret;
  1229. }
  1230. static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
  1231. struct file *out, loff_t *ppos,
  1232. size_t len, unsigned int flags)
  1233. {
  1234. ssize_t ret;
  1235. gfs2_size_hint(out, *ppos, len);
  1236. ret = iter_file_splice_write(pipe, out, ppos, len, flags);
  1237. return ret;
  1238. }
  1239. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  1240. /**
  1241. * gfs2_lock - acquire/release a posix lock on a file
  1242. * @file: the file pointer
  1243. * @cmd: either modify or retrieve lock state, possibly wait
  1244. * @fl: type and range of lock
  1245. *
  1246. * Returns: errno
  1247. */
  1248. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  1249. {
  1250. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  1251. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  1252. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1253. if (!(fl->c.flc_flags & FL_POSIX))
  1254. return -ENOLCK;
  1255. if (gfs2_withdrawing_or_withdrawn(sdp)) {
  1256. if (lock_is_unlock(fl))
  1257. locks_lock_file_wait(file, fl);
  1258. return -EIO;
  1259. }
  1260. if (cmd == F_CANCELLK)
  1261. return dlm_posix_cancel(ls->ls_dlm, ip->i_no_addr, file, fl);
  1262. else if (IS_GETLK(cmd))
  1263. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  1264. else if (lock_is_unlock(fl))
  1265. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  1266. else
  1267. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  1268. }
  1269. static void __flock_holder_uninit(struct file *file, struct gfs2_holder *fl_gh)
  1270. {
  1271. struct gfs2_glock *gl = gfs2_glock_hold(fl_gh->gh_gl);
  1272. /*
  1273. * Make sure gfs2_glock_put() won't sleep under the file->f_lock
  1274. * spinlock.
  1275. */
  1276. spin_lock(&file->f_lock);
  1277. gfs2_holder_uninit(fl_gh);
  1278. spin_unlock(&file->f_lock);
  1279. gfs2_glock_put(gl);
  1280. }
  1281. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  1282. {
  1283. struct gfs2_file *fp = file->private_data;
  1284. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1285. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  1286. struct gfs2_glock *gl;
  1287. unsigned int state;
  1288. u16 flags;
  1289. int error = 0;
  1290. int sleeptime;
  1291. state = lock_is_write(fl) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  1292. flags = GL_EXACT | GL_NOPID;
  1293. if (!IS_SETLKW(cmd))
  1294. flags |= LM_FLAG_TRY_1CB;
  1295. mutex_lock(&fp->f_fl_mutex);
  1296. if (gfs2_holder_initialized(fl_gh)) {
  1297. struct file_lock request;
  1298. if (fl_gh->gh_state == state)
  1299. goto out;
  1300. locks_init_lock(&request);
  1301. request.c.flc_type = F_UNLCK;
  1302. request.c.flc_flags = FL_FLOCK;
  1303. locks_lock_file_wait(file, &request);
  1304. gfs2_glock_dq(fl_gh);
  1305. gfs2_holder_reinit(state, flags, fl_gh);
  1306. } else {
  1307. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  1308. &gfs2_flock_glops, CREATE, &gl);
  1309. if (error)
  1310. goto out;
  1311. spin_lock(&file->f_lock);
  1312. gfs2_holder_init(gl, state, flags, fl_gh);
  1313. spin_unlock(&file->f_lock);
  1314. gfs2_glock_put(gl);
  1315. }
  1316. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  1317. error = gfs2_glock_nq(fl_gh);
  1318. if (error != GLR_TRYFAILED)
  1319. break;
  1320. fl_gh->gh_flags &= ~LM_FLAG_TRY_1CB;
  1321. fl_gh->gh_flags |= LM_FLAG_TRY;
  1322. msleep(sleeptime);
  1323. }
  1324. if (error) {
  1325. __flock_holder_uninit(file, fl_gh);
  1326. if (error == GLR_TRYFAILED)
  1327. error = -EAGAIN;
  1328. } else {
  1329. error = locks_lock_file_wait(file, fl);
  1330. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1331. }
  1332. out:
  1333. mutex_unlock(&fp->f_fl_mutex);
  1334. return error;
  1335. }
  1336. static void do_unflock(struct file *file, struct file_lock *fl)
  1337. {
  1338. struct gfs2_file *fp = file->private_data;
  1339. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1340. mutex_lock(&fp->f_fl_mutex);
  1341. locks_lock_file_wait(file, fl);
  1342. if (gfs2_holder_initialized(fl_gh)) {
  1343. gfs2_glock_dq(fl_gh);
  1344. __flock_holder_uninit(file, fl_gh);
  1345. }
  1346. mutex_unlock(&fp->f_fl_mutex);
  1347. }
  1348. /**
  1349. * gfs2_flock - acquire/release a flock lock on a file
  1350. * @file: the file pointer
  1351. * @cmd: either modify or retrieve lock state, possibly wait
  1352. * @fl: type and range of lock
  1353. *
  1354. * Returns: errno
  1355. */
  1356. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  1357. {
  1358. if (!(fl->c.flc_flags & FL_FLOCK))
  1359. return -ENOLCK;
  1360. if (lock_is_unlock(fl)) {
  1361. do_unflock(file, fl);
  1362. return 0;
  1363. } else {
  1364. return do_flock(file, cmd, fl);
  1365. }
  1366. }
  1367. const struct file_operations gfs2_file_fops = {
  1368. .llseek = gfs2_llseek,
  1369. .read_iter = gfs2_file_read_iter,
  1370. .write_iter = gfs2_file_write_iter,
  1371. .iopoll = iocb_bio_iopoll,
  1372. .unlocked_ioctl = gfs2_ioctl,
  1373. .compat_ioctl = gfs2_compat_ioctl,
  1374. .mmap = gfs2_mmap,
  1375. .open = gfs2_open,
  1376. .release = gfs2_release,
  1377. .fsync = gfs2_fsync,
  1378. .lock = gfs2_lock,
  1379. .flock = gfs2_flock,
  1380. .splice_read = copy_splice_read,
  1381. .splice_write = gfs2_file_splice_write,
  1382. .setlease = simple_nosetlease,
  1383. .fallocate = gfs2_fallocate,
  1384. };
  1385. const struct file_operations gfs2_dir_fops = {
  1386. .iterate_shared = gfs2_readdir,
  1387. .unlocked_ioctl = gfs2_ioctl,
  1388. .compat_ioctl = gfs2_compat_ioctl,
  1389. .open = gfs2_open,
  1390. .release = gfs2_release,
  1391. .fsync = gfs2_fsync,
  1392. .lock = gfs2_lock,
  1393. .flock = gfs2_flock,
  1394. .llseek = default_llseek,
  1395. };
  1396. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  1397. const struct file_operations gfs2_file_fops_nolock = {
  1398. .llseek = gfs2_llseek,
  1399. .read_iter = gfs2_file_read_iter,
  1400. .write_iter = gfs2_file_write_iter,
  1401. .iopoll = iocb_bio_iopoll,
  1402. .unlocked_ioctl = gfs2_ioctl,
  1403. .compat_ioctl = gfs2_compat_ioctl,
  1404. .mmap = gfs2_mmap,
  1405. .open = gfs2_open,
  1406. .release = gfs2_release,
  1407. .fsync = gfs2_fsync,
  1408. .splice_read = copy_splice_read,
  1409. .splice_write = gfs2_file_splice_write,
  1410. .setlease = generic_setlease,
  1411. .fallocate = gfs2_fallocate,
  1412. };
  1413. const struct file_operations gfs2_dir_fops_nolock = {
  1414. .iterate_shared = gfs2_readdir,
  1415. .unlocked_ioctl = gfs2_ioctl,
  1416. .compat_ioctl = gfs2_compat_ioctl,
  1417. .open = gfs2_open,
  1418. .release = gfs2_release,
  1419. .fsync = gfs2_fsync,
  1420. .llseek = default_llseek,
  1421. };