file.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/uio.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/mm.h>
  17. #include <linux/mount.h>
  18. #include <linux/fs.h>
  19. #include <linux/gfs2_ondisk.h>
  20. #include <linux/falloc.h>
  21. #include <linux/swap.h>
  22. #include <linux/crc32.h>
  23. #include <linux/writeback.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/dlm.h>
  26. #include <linux/dlm_plock.h>
  27. #include <linux/delay.h>
  28. #include <linux/backing-dev.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. * 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 int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  126. {
  127. struct inode *inode = file_inode(filp);
  128. struct gfs2_inode *ip = GFS2_I(inode);
  129. struct gfs2_holder gh;
  130. int i, error;
  131. u32 gfsflags, fsflags = 0;
  132. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  133. error = gfs2_glock_nq(&gh);
  134. if (error)
  135. goto out_uninit;
  136. gfsflags = ip->i_diskflags;
  137. if (S_ISDIR(inode->i_mode))
  138. gfsflags &= ~GFS2_DIF_JDATA;
  139. else
  140. gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
  141. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
  142. if (gfsflags & fsflag_gfs2flag[i].gfsflag)
  143. fsflags |= fsflag_gfs2flag[i].fsflag;
  144. if (put_user(fsflags, ptr))
  145. error = -EFAULT;
  146. gfs2_glock_dq(&gh);
  147. out_uninit:
  148. gfs2_holder_uninit(&gh);
  149. return error;
  150. }
  151. void gfs2_set_inode_flags(struct inode *inode)
  152. {
  153. struct gfs2_inode *ip = GFS2_I(inode);
  154. unsigned int flags = inode->i_flags;
  155. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  156. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  157. flags |= S_NOSEC;
  158. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  159. flags |= S_IMMUTABLE;
  160. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  161. flags |= S_APPEND;
  162. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  163. flags |= S_NOATIME;
  164. if (ip->i_diskflags & GFS2_DIF_SYNC)
  165. flags |= S_SYNC;
  166. inode->i_flags = flags;
  167. }
  168. /* Flags that can be set by user space */
  169. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  170. GFS2_DIF_IMMUTABLE| \
  171. GFS2_DIF_APPENDONLY| \
  172. GFS2_DIF_NOATIME| \
  173. GFS2_DIF_SYNC| \
  174. GFS2_DIF_TOPDIR| \
  175. GFS2_DIF_INHERIT_JDATA)
  176. /**
  177. * do_gfs2_set_flags - set flags on an inode
  178. * @filp: file pointer
  179. * @reqflags: The flags to set
  180. * @mask: Indicates which flags are valid
  181. *
  182. */
  183. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  184. {
  185. struct inode *inode = file_inode(filp);
  186. struct gfs2_inode *ip = GFS2_I(inode);
  187. struct gfs2_sbd *sdp = GFS2_SB(inode);
  188. struct buffer_head *bh;
  189. struct gfs2_holder gh;
  190. int error;
  191. u32 new_flags, flags;
  192. error = mnt_want_write_file(filp);
  193. if (error)
  194. return error;
  195. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  196. if (error)
  197. goto out_drop_write;
  198. error = -EACCES;
  199. if (!inode_owner_or_capable(inode))
  200. goto out;
  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. error = -EPERM;
  207. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  208. goto out;
  209. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  210. goto out;
  211. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  212. !capable(CAP_LINUX_IMMUTABLE))
  213. goto out;
  214. if (!IS_IMMUTABLE(inode)) {
  215. error = gfs2_permission(inode, MAY_WRITE);
  216. if (error)
  217. goto out;
  218. }
  219. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  220. if (new_flags & GFS2_DIF_JDATA)
  221. gfs2_log_flush(sdp, ip->i_gl,
  222. GFS2_LOG_HEAD_FLUSH_NORMAL |
  223. GFS2_LFC_SET_FLAGS);
  224. error = filemap_fdatawrite(inode->i_mapping);
  225. if (error)
  226. goto out;
  227. error = filemap_fdatawait(inode->i_mapping);
  228. if (error)
  229. goto out;
  230. if (new_flags & GFS2_DIF_JDATA)
  231. gfs2_ordered_del_inode(ip);
  232. }
  233. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  234. if (error)
  235. goto out;
  236. error = gfs2_meta_inode_buffer(ip, &bh);
  237. if (error)
  238. goto out_trans_end;
  239. inode->i_ctime = current_time(inode);
  240. gfs2_trans_add_meta(ip->i_gl, bh);
  241. ip->i_diskflags = new_flags;
  242. gfs2_dinode_out(ip, bh->b_data);
  243. brelse(bh);
  244. gfs2_set_inode_flags(inode);
  245. gfs2_set_aops(inode);
  246. out_trans_end:
  247. gfs2_trans_end(sdp);
  248. out:
  249. gfs2_glock_dq_uninit(&gh);
  250. out_drop_write:
  251. mnt_drop_write_file(filp);
  252. return error;
  253. }
  254. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  255. {
  256. struct inode *inode = file_inode(filp);
  257. u32 fsflags, gfsflags = 0;
  258. u32 mask;
  259. int i;
  260. if (get_user(fsflags, ptr))
  261. return -EFAULT;
  262. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
  263. if (fsflags & fsflag_gfs2flag[i].fsflag) {
  264. fsflags &= ~fsflag_gfs2flag[i].fsflag;
  265. gfsflags |= fsflag_gfs2flag[i].gfsflag;
  266. }
  267. }
  268. if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
  269. return -EINVAL;
  270. mask = GFS2_FLAGS_USER_SET;
  271. if (S_ISDIR(inode->i_mode)) {
  272. mask &= ~GFS2_DIF_JDATA;
  273. } else {
  274. /* The GFS2_DIF_TOPDIR flag is only valid for directories. */
  275. if (gfsflags & GFS2_DIF_TOPDIR)
  276. return -EINVAL;
  277. mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
  278. }
  279. return do_gfs2_set_flags(filp, gfsflags, mask);
  280. }
  281. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  282. {
  283. switch(cmd) {
  284. case FS_IOC_GETFLAGS:
  285. return gfs2_get_flags(filp, (u32 __user *)arg);
  286. case FS_IOC_SETFLAGS:
  287. return gfs2_set_flags(filp, (u32 __user *)arg);
  288. case FITRIM:
  289. return gfs2_fitrim(filp, (void __user *)arg);
  290. }
  291. return -ENOTTY;
  292. }
  293. /**
  294. * gfs2_size_hint - Give a hint to the size of a write request
  295. * @filep: The struct file
  296. * @offset: The file offset of the write
  297. * @size: The length of the write
  298. *
  299. * When we are about to do a write, this function records the total
  300. * write size in order to provide a suitable hint to the lower layers
  301. * about how many blocks will be required.
  302. *
  303. */
  304. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  305. {
  306. struct inode *inode = file_inode(filep);
  307. struct gfs2_sbd *sdp = GFS2_SB(inode);
  308. struct gfs2_inode *ip = GFS2_I(inode);
  309. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  310. int hint = min_t(size_t, INT_MAX, blks);
  311. if (hint > atomic_read(&ip->i_res.rs_sizehint))
  312. atomic_set(&ip->i_res.rs_sizehint, hint);
  313. }
  314. /**
  315. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  316. * @page: The (locked) page to allocate backing for
  317. *
  318. * We try to allocate all the blocks required for the page in
  319. * one go. This might fail for various reasons, so we keep
  320. * trying until all the blocks to back this page are allocated.
  321. * If some of the blocks are already allocated, thats ok too.
  322. */
  323. static int gfs2_allocate_page_backing(struct page *page)
  324. {
  325. struct inode *inode = page->mapping->host;
  326. struct buffer_head bh;
  327. unsigned long size = PAGE_SIZE;
  328. u64 lblock = page->index << (PAGE_SHIFT - inode->i_blkbits);
  329. do {
  330. bh.b_state = 0;
  331. bh.b_size = size;
  332. gfs2_block_map(inode, lblock, &bh, 1);
  333. if (!buffer_mapped(&bh))
  334. return -EIO;
  335. size -= bh.b_size;
  336. lblock += (bh.b_size >> inode->i_blkbits);
  337. } while(size > 0);
  338. return 0;
  339. }
  340. /**
  341. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  342. * @vma: The virtual memory area
  343. * @vmf: The virtual memory fault containing the page to become writable
  344. *
  345. * When the page becomes writable, we need to ensure that we have
  346. * blocks allocated on disk to back that page.
  347. */
  348. static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
  349. {
  350. struct page *page = vmf->page;
  351. struct inode *inode = file_inode(vmf->vma->vm_file);
  352. struct gfs2_inode *ip = GFS2_I(inode);
  353. struct gfs2_sbd *sdp = GFS2_SB(inode);
  354. struct gfs2_alloc_parms ap = { .aflags = 0, };
  355. unsigned long last_index;
  356. u64 pos = page->index << PAGE_SHIFT;
  357. unsigned int data_blocks, ind_blocks, rblocks;
  358. struct gfs2_holder gh;
  359. loff_t size;
  360. int ret;
  361. sb_start_pagefault(inode->i_sb);
  362. ret = gfs2_rsqa_alloc(ip);
  363. if (ret)
  364. goto out;
  365. gfs2_size_hint(vmf->vma->vm_file, pos, PAGE_SIZE);
  366. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  367. ret = gfs2_glock_nq(&gh);
  368. if (ret)
  369. goto out_uninit;
  370. /* Update file times before taking page lock */
  371. file_update_time(vmf->vma->vm_file);
  372. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  373. set_bit(GIF_SW_PAGED, &ip->i_flags);
  374. if (!gfs2_write_alloc_required(ip, pos, PAGE_SIZE)) {
  375. lock_page(page);
  376. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  377. ret = -EAGAIN;
  378. unlock_page(page);
  379. }
  380. goto out_unlock;
  381. }
  382. ret = gfs2_rindex_update(sdp);
  383. if (ret)
  384. goto out_unlock;
  385. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  386. ap.target = data_blocks + ind_blocks;
  387. ret = gfs2_quota_lock_check(ip, &ap);
  388. if (ret)
  389. goto out_unlock;
  390. ret = gfs2_inplace_reserve(ip, &ap);
  391. if (ret)
  392. goto out_quota_unlock;
  393. rblocks = RES_DINODE + ind_blocks;
  394. if (gfs2_is_jdata(ip))
  395. rblocks += data_blocks ? data_blocks : 1;
  396. if (ind_blocks || data_blocks) {
  397. rblocks += RES_STATFS + RES_QUOTA;
  398. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  399. }
  400. ret = gfs2_trans_begin(sdp, rblocks, 0);
  401. if (ret)
  402. goto out_trans_fail;
  403. lock_page(page);
  404. ret = -EINVAL;
  405. size = i_size_read(inode);
  406. last_index = (size - 1) >> PAGE_SHIFT;
  407. /* Check page index against inode size */
  408. if (size == 0 || (page->index > last_index))
  409. goto out_trans_end;
  410. ret = -EAGAIN;
  411. /* If truncated, we must retry the operation, we may have raced
  412. * with the glock demotion code.
  413. */
  414. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  415. goto out_trans_end;
  416. /* Unstuff, if required, and allocate backing blocks for page */
  417. ret = 0;
  418. if (gfs2_is_stuffed(ip))
  419. ret = gfs2_unstuff_dinode(ip, page);
  420. if (ret == 0)
  421. ret = gfs2_allocate_page_backing(page);
  422. out_trans_end:
  423. if (ret)
  424. unlock_page(page);
  425. gfs2_trans_end(sdp);
  426. out_trans_fail:
  427. gfs2_inplace_release(ip);
  428. out_quota_unlock:
  429. gfs2_quota_unlock(ip);
  430. out_unlock:
  431. gfs2_glock_dq(&gh);
  432. out_uninit:
  433. gfs2_holder_uninit(&gh);
  434. if (ret == 0) {
  435. set_page_dirty(page);
  436. wait_for_stable_page(page);
  437. }
  438. out:
  439. sb_end_pagefault(inode->i_sb);
  440. return block_page_mkwrite_return(ret);
  441. }
  442. static const struct vm_operations_struct gfs2_vm_ops = {
  443. .fault = filemap_fault,
  444. .map_pages = filemap_map_pages,
  445. .page_mkwrite = gfs2_page_mkwrite,
  446. };
  447. /**
  448. * gfs2_mmap -
  449. * @file: The file to map
  450. * @vma: The VMA which described the mapping
  451. *
  452. * There is no need to get a lock here unless we should be updating
  453. * atime. We ignore any locking errors since the only consequence is
  454. * a missed atime update (which will just be deferred until later).
  455. *
  456. * Returns: 0
  457. */
  458. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  459. {
  460. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  461. if (!(file->f_flags & O_NOATIME) &&
  462. !IS_NOATIME(&ip->i_inode)) {
  463. struct gfs2_holder i_gh;
  464. int error;
  465. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  466. &i_gh);
  467. if (error)
  468. return error;
  469. /* grab lock to update inode */
  470. gfs2_glock_dq_uninit(&i_gh);
  471. file_accessed(file);
  472. }
  473. vma->vm_ops = &gfs2_vm_ops;
  474. return 0;
  475. }
  476. /**
  477. * gfs2_open_common - This is common to open and atomic_open
  478. * @inode: The inode being opened
  479. * @file: The file being opened
  480. *
  481. * This maybe called under a glock or not depending upon how it has
  482. * been called. We must always be called under a glock for regular
  483. * files, however. For other file types, it does not matter whether
  484. * we hold the glock or not.
  485. *
  486. * Returns: Error code or 0 for success
  487. */
  488. int gfs2_open_common(struct inode *inode, struct file *file)
  489. {
  490. struct gfs2_file *fp;
  491. int ret;
  492. if (S_ISREG(inode->i_mode)) {
  493. ret = generic_file_open(inode, file);
  494. if (ret)
  495. return ret;
  496. }
  497. fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
  498. if (!fp)
  499. return -ENOMEM;
  500. mutex_init(&fp->f_fl_mutex);
  501. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  502. file->private_data = fp;
  503. return 0;
  504. }
  505. /**
  506. * gfs2_open - open a file
  507. * @inode: the inode to open
  508. * @file: the struct file for this opening
  509. *
  510. * After atomic_open, this function is only used for opening files
  511. * which are already cached. We must still get the glock for regular
  512. * files to ensure that we have the file size uptodate for the large
  513. * file check which is in the common code. That is only an issue for
  514. * regular files though.
  515. *
  516. * Returns: errno
  517. */
  518. static int gfs2_open(struct inode *inode, struct file *file)
  519. {
  520. struct gfs2_inode *ip = GFS2_I(inode);
  521. struct gfs2_holder i_gh;
  522. int error;
  523. bool need_unlock = false;
  524. if (S_ISREG(ip->i_inode.i_mode)) {
  525. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  526. &i_gh);
  527. if (error)
  528. return error;
  529. need_unlock = true;
  530. }
  531. error = gfs2_open_common(inode, file);
  532. if (need_unlock)
  533. gfs2_glock_dq_uninit(&i_gh);
  534. return error;
  535. }
  536. /**
  537. * gfs2_release - called to close a struct file
  538. * @inode: the inode the struct file belongs to
  539. * @file: the struct file being closed
  540. *
  541. * Returns: errno
  542. */
  543. static int gfs2_release(struct inode *inode, struct file *file)
  544. {
  545. struct gfs2_inode *ip = GFS2_I(inode);
  546. kfree(file->private_data);
  547. file->private_data = NULL;
  548. if (!(file->f_mode & FMODE_WRITE))
  549. return 0;
  550. gfs2_rsqa_delete(ip, &inode->i_writecount);
  551. return 0;
  552. }
  553. /**
  554. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  555. * @file: the file that points to the dentry
  556. * @start: the start position in the file to sync
  557. * @end: the end position in the file to sync
  558. * @datasync: set if we can ignore timestamp changes
  559. *
  560. * We split the data flushing here so that we don't wait for the data
  561. * until after we've also sent the metadata to disk. Note that for
  562. * data=ordered, we will write & wait for the data at the log flush
  563. * stage anyway, so this is unlikely to make much of a difference
  564. * except in the data=writeback case.
  565. *
  566. * If the fdatawrite fails due to any reason except -EIO, we will
  567. * continue the remainder of the fsync, although we'll still report
  568. * the error at the end. This is to match filemap_write_and_wait_range()
  569. * behaviour.
  570. *
  571. * Returns: errno
  572. */
  573. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  574. int datasync)
  575. {
  576. struct address_space *mapping = file->f_mapping;
  577. struct inode *inode = mapping->host;
  578. int sync_state = inode->i_state & I_DIRTY_ALL;
  579. struct gfs2_inode *ip = GFS2_I(inode);
  580. int ret = 0, ret1 = 0;
  581. if (mapping->nrpages) {
  582. ret1 = filemap_fdatawrite_range(mapping, start, end);
  583. if (ret1 == -EIO)
  584. return ret1;
  585. }
  586. if (!gfs2_is_jdata(ip))
  587. sync_state &= ~I_DIRTY_PAGES;
  588. if (datasync)
  589. sync_state &= ~(I_DIRTY_SYNC | I_DIRTY_TIME);
  590. if (sync_state) {
  591. ret = sync_inode_metadata(inode, 1);
  592. if (ret)
  593. return ret;
  594. if (gfs2_is_jdata(ip))
  595. ret = file_write_and_wait(file);
  596. if (ret)
  597. return ret;
  598. gfs2_ail_flush(ip->i_gl, 1);
  599. }
  600. if (mapping->nrpages)
  601. ret = file_fdatawait_range(file, start, end);
  602. return ret ? ret : ret1;
  603. }
  604. static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to)
  605. {
  606. struct file *file = iocb->ki_filp;
  607. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  608. size_t count = iov_iter_count(to);
  609. struct gfs2_holder gh;
  610. ssize_t ret;
  611. if (!count)
  612. return 0; /* skip atime */
  613. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  614. ret = gfs2_glock_nq(&gh);
  615. if (ret)
  616. goto out_uninit;
  617. ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL);
  618. gfs2_glock_dq(&gh);
  619. out_uninit:
  620. gfs2_holder_uninit(&gh);
  621. return ret;
  622. }
  623. static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
  624. {
  625. struct file *file = iocb->ki_filp;
  626. struct inode *inode = file->f_mapping->host;
  627. struct gfs2_inode *ip = GFS2_I(inode);
  628. size_t len = iov_iter_count(from);
  629. loff_t offset = iocb->ki_pos;
  630. struct gfs2_holder gh;
  631. ssize_t ret;
  632. /*
  633. * Deferred lock, even if its a write, since we do no allocation on
  634. * this path. All we need to change is the atime, and this lock mode
  635. * ensures that other nodes have flushed their buffered read caches
  636. * (i.e. their page cache entries for this inode). We do not,
  637. * unfortunately, have the option of only flushing a range like the
  638. * VFS does.
  639. */
  640. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  641. ret = gfs2_glock_nq(&gh);
  642. if (ret)
  643. goto out_uninit;
  644. /* Silently fall back to buffered I/O when writing beyond EOF */
  645. if (offset + len > i_size_read(&ip->i_inode))
  646. goto out;
  647. ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL);
  648. out:
  649. gfs2_glock_dq(&gh);
  650. out_uninit:
  651. gfs2_holder_uninit(&gh);
  652. return ret;
  653. }
  654. static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  655. {
  656. ssize_t ret;
  657. if (iocb->ki_flags & IOCB_DIRECT) {
  658. ret = gfs2_file_direct_read(iocb, to);
  659. if (likely(ret != -ENOTBLK))
  660. return ret;
  661. iocb->ki_flags &= ~IOCB_DIRECT;
  662. }
  663. return generic_file_read_iter(iocb, to);
  664. }
  665. /**
  666. * gfs2_file_write_iter - Perform a write to a file
  667. * @iocb: The io context
  668. * @from: The data to write
  669. *
  670. * We have to do a lock/unlock here to refresh the inode size for
  671. * O_APPEND writes, otherwise we can land up writing at the wrong
  672. * offset. There is still a race, but provided the app is using its
  673. * own file locking, this will make O_APPEND work as expected.
  674. *
  675. */
  676. static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  677. {
  678. struct file *file = iocb->ki_filp;
  679. struct inode *inode = file_inode(file);
  680. struct gfs2_inode *ip = GFS2_I(inode);
  681. ssize_t ret;
  682. ret = gfs2_rsqa_alloc(ip);
  683. if (ret)
  684. return ret;
  685. gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
  686. if (iocb->ki_flags & IOCB_APPEND) {
  687. struct gfs2_holder gh;
  688. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  689. if (ret)
  690. return ret;
  691. gfs2_glock_dq_uninit(&gh);
  692. }
  693. inode_lock(inode);
  694. ret = generic_write_checks(iocb, from);
  695. if (ret <= 0)
  696. goto out_unlock;
  697. ret = file_remove_privs(file);
  698. if (ret)
  699. goto out_unlock;
  700. ret = file_update_time(file);
  701. if (ret)
  702. goto out_unlock;
  703. if (iocb->ki_flags & IOCB_DIRECT) {
  704. struct address_space *mapping = file->f_mapping;
  705. ssize_t buffered, ret2;
  706. ret = gfs2_file_direct_write(iocb, from);
  707. if (ret < 0 || !iov_iter_count(from))
  708. goto out_unlock;
  709. iocb->ki_flags |= IOCB_DSYNC;
  710. current->backing_dev_info = inode_to_bdi(inode);
  711. buffered = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
  712. current->backing_dev_info = NULL;
  713. if (unlikely(buffered <= 0))
  714. goto out_unlock;
  715. /*
  716. * We need to ensure that the page cache pages are written to
  717. * disk and invalidated to preserve the expected O_DIRECT
  718. * semantics. If the writeback or invalidate fails, only report
  719. * the direct I/O range as we don't know if the buffered pages
  720. * made it to disk.
  721. */
  722. iocb->ki_pos += buffered;
  723. ret2 = generic_write_sync(iocb, buffered);
  724. invalidate_mapping_pages(mapping,
  725. (iocb->ki_pos - buffered) >> PAGE_SHIFT,
  726. (iocb->ki_pos - 1) >> PAGE_SHIFT);
  727. if (!ret || ret2 > 0)
  728. ret += ret2;
  729. } else {
  730. current->backing_dev_info = inode_to_bdi(inode);
  731. ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
  732. current->backing_dev_info = NULL;
  733. if (likely(ret > 0)) {
  734. iocb->ki_pos += ret;
  735. ret = generic_write_sync(iocb, ret);
  736. }
  737. }
  738. out_unlock:
  739. inode_unlock(inode);
  740. return ret;
  741. }
  742. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  743. int mode)
  744. {
  745. struct super_block *sb = inode->i_sb;
  746. struct gfs2_inode *ip = GFS2_I(inode);
  747. loff_t end = offset + len;
  748. struct buffer_head *dibh;
  749. int error;
  750. error = gfs2_meta_inode_buffer(ip, &dibh);
  751. if (unlikely(error))
  752. return error;
  753. gfs2_trans_add_meta(ip->i_gl, dibh);
  754. if (gfs2_is_stuffed(ip)) {
  755. error = gfs2_unstuff_dinode(ip, NULL);
  756. if (unlikely(error))
  757. goto out;
  758. }
  759. while (offset < end) {
  760. struct iomap iomap = { };
  761. error = gfs2_iomap_get_alloc(inode, offset, end - offset,
  762. &iomap);
  763. if (error)
  764. goto out;
  765. offset = iomap.offset + iomap.length;
  766. if (!(iomap.flags & IOMAP_F_NEW))
  767. continue;
  768. error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
  769. iomap.length >> inode->i_blkbits,
  770. GFP_NOFS);
  771. if (error) {
  772. fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
  773. goto out;
  774. }
  775. }
  776. out:
  777. brelse(dibh);
  778. return error;
  779. }
  780. /**
  781. * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
  782. * blocks, determine how many bytes can be written.
  783. * @ip: The inode in question.
  784. * @len: Max cap of bytes. What we return in *len must be <= this.
  785. * @data_blocks: Compute and return the number of data blocks needed
  786. * @ind_blocks: Compute and return the number of indirect blocks needed
  787. * @max_blocks: The total blocks available to work with.
  788. *
  789. * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
  790. */
  791. static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
  792. unsigned int *data_blocks, unsigned int *ind_blocks,
  793. unsigned int max_blocks)
  794. {
  795. loff_t max = *len;
  796. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  797. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  798. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  799. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  800. max_data -= tmp;
  801. }
  802. *data_blocks = max_data;
  803. *ind_blocks = max_blocks - max_data;
  804. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  805. if (*len > max) {
  806. *len = max;
  807. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  808. }
  809. }
  810. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  811. {
  812. struct inode *inode = file_inode(file);
  813. struct gfs2_sbd *sdp = GFS2_SB(inode);
  814. struct gfs2_inode *ip = GFS2_I(inode);
  815. struct gfs2_alloc_parms ap = { .aflags = 0, };
  816. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  817. loff_t bytes, max_bytes, max_blks;
  818. int error;
  819. const loff_t pos = offset;
  820. const loff_t count = len;
  821. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  822. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  823. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  824. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  825. offset &= bsize_mask;
  826. len = next - offset;
  827. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  828. if (!bytes)
  829. bytes = UINT_MAX;
  830. bytes &= bsize_mask;
  831. if (bytes == 0)
  832. bytes = sdp->sd_sb.sb_bsize;
  833. gfs2_size_hint(file, offset, len);
  834. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  835. ap.min_target = data_blocks + ind_blocks;
  836. while (len > 0) {
  837. if (len < bytes)
  838. bytes = len;
  839. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  840. len -= bytes;
  841. offset += bytes;
  842. continue;
  843. }
  844. /* We need to determine how many bytes we can actually
  845. * fallocate without exceeding quota or going over the
  846. * end of the fs. We start off optimistically by assuming
  847. * we can write max_bytes */
  848. max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
  849. /* Since max_bytes is most likely a theoretical max, we
  850. * calculate a more realistic 'bytes' to serve as a good
  851. * starting point for the number of bytes we may be able
  852. * to write */
  853. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  854. ap.target = data_blocks + ind_blocks;
  855. error = gfs2_quota_lock_check(ip, &ap);
  856. if (error)
  857. return error;
  858. /* ap.allowed tells us how many blocks quota will allow
  859. * us to write. Check if this reduces max_blks */
  860. max_blks = UINT_MAX;
  861. if (ap.allowed)
  862. max_blks = ap.allowed;
  863. error = gfs2_inplace_reserve(ip, &ap);
  864. if (error)
  865. goto out_qunlock;
  866. /* check if the selected rgrp limits our max_blks further */
  867. if (ap.allowed && ap.allowed < max_blks)
  868. max_blks = ap.allowed;
  869. /* Almost done. Calculate bytes that can be written using
  870. * max_blks. We also recompute max_bytes, data_blocks and
  871. * ind_blocks */
  872. calc_max_reserv(ip, &max_bytes, &data_blocks,
  873. &ind_blocks, max_blks);
  874. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  875. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  876. if (gfs2_is_jdata(ip))
  877. rblocks += data_blocks ? data_blocks : 1;
  878. error = gfs2_trans_begin(sdp, rblocks,
  879. PAGE_SIZE/sdp->sd_sb.sb_bsize);
  880. if (error)
  881. goto out_trans_fail;
  882. error = fallocate_chunk(inode, offset, max_bytes, mode);
  883. gfs2_trans_end(sdp);
  884. if (error)
  885. goto out_trans_fail;
  886. len -= max_bytes;
  887. offset += max_bytes;
  888. gfs2_inplace_release(ip);
  889. gfs2_quota_unlock(ip);
  890. }
  891. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size) {
  892. i_size_write(inode, pos + count);
  893. file_update_time(file);
  894. mark_inode_dirty(inode);
  895. }
  896. if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
  897. return vfs_fsync_range(file, pos, pos + count - 1,
  898. (file->f_flags & __O_SYNC) ? 0 : 1);
  899. return 0;
  900. out_trans_fail:
  901. gfs2_inplace_release(ip);
  902. out_qunlock:
  903. gfs2_quota_unlock(ip);
  904. return error;
  905. }
  906. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  907. {
  908. struct inode *inode = file_inode(file);
  909. struct gfs2_sbd *sdp = GFS2_SB(inode);
  910. struct gfs2_inode *ip = GFS2_I(inode);
  911. struct gfs2_holder gh;
  912. int ret;
  913. if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
  914. return -EOPNOTSUPP;
  915. /* fallocate is needed by gfs2_grow to reserve space in the rindex */
  916. if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
  917. return -EOPNOTSUPP;
  918. inode_lock(inode);
  919. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  920. ret = gfs2_glock_nq(&gh);
  921. if (ret)
  922. goto out_uninit;
  923. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  924. (offset + len) > inode->i_size) {
  925. ret = inode_newsize_ok(inode, offset + len);
  926. if (ret)
  927. goto out_unlock;
  928. }
  929. ret = get_write_access(inode);
  930. if (ret)
  931. goto out_unlock;
  932. if (mode & FALLOC_FL_PUNCH_HOLE) {
  933. ret = __gfs2_punch_hole(file, offset, len);
  934. } else {
  935. ret = gfs2_rsqa_alloc(ip);
  936. if (ret)
  937. goto out_putw;
  938. ret = __gfs2_fallocate(file, mode, offset, len);
  939. if (ret)
  940. gfs2_rs_deltree(&ip->i_res);
  941. }
  942. out_putw:
  943. put_write_access(inode);
  944. out_unlock:
  945. gfs2_glock_dq(&gh);
  946. out_uninit:
  947. gfs2_holder_uninit(&gh);
  948. inode_unlock(inode);
  949. return ret;
  950. }
  951. static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
  952. struct file *out, loff_t *ppos,
  953. size_t len, unsigned int flags)
  954. {
  955. int error;
  956. struct gfs2_inode *ip = GFS2_I(out->f_mapping->host);
  957. error = gfs2_rsqa_alloc(ip);
  958. if (error)
  959. return (ssize_t)error;
  960. gfs2_size_hint(out, *ppos, len);
  961. return iter_file_splice_write(pipe, out, ppos, len, flags);
  962. }
  963. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  964. /**
  965. * gfs2_lock - acquire/release a posix lock on a file
  966. * @file: the file pointer
  967. * @cmd: either modify or retrieve lock state, possibly wait
  968. * @fl: type and range of lock
  969. *
  970. * Returns: errno
  971. */
  972. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  973. {
  974. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  975. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  976. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  977. if (!(fl->fl_flags & FL_POSIX))
  978. return -ENOLCK;
  979. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  980. return -ENOLCK;
  981. if (cmd == F_CANCELLK) {
  982. /* Hack: */
  983. cmd = F_SETLK;
  984. fl->fl_type = F_UNLCK;
  985. }
  986. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  987. if (fl->fl_type == F_UNLCK)
  988. locks_lock_file_wait(file, fl);
  989. return -EIO;
  990. }
  991. if (IS_GETLK(cmd))
  992. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  993. else if (fl->fl_type == F_UNLCK)
  994. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  995. else
  996. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  997. }
  998. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  999. {
  1000. struct gfs2_file *fp = file->private_data;
  1001. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1002. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  1003. struct gfs2_glock *gl;
  1004. unsigned int state;
  1005. u16 flags;
  1006. int error = 0;
  1007. int sleeptime;
  1008. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  1009. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
  1010. mutex_lock(&fp->f_fl_mutex);
  1011. if (gfs2_holder_initialized(fl_gh)) {
  1012. if (fl_gh->gh_state == state)
  1013. goto out;
  1014. locks_lock_file_wait(file,
  1015. &(struct file_lock) {
  1016. .fl_type = F_UNLCK,
  1017. .fl_flags = FL_FLOCK
  1018. });
  1019. gfs2_glock_dq(fl_gh);
  1020. gfs2_holder_reinit(state, flags, fl_gh);
  1021. } else {
  1022. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  1023. &gfs2_flock_glops, CREATE, &gl);
  1024. if (error)
  1025. goto out;
  1026. gfs2_holder_init(gl, state, flags, fl_gh);
  1027. gfs2_glock_put(gl);
  1028. }
  1029. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  1030. error = gfs2_glock_nq(fl_gh);
  1031. if (error != GLR_TRYFAILED)
  1032. break;
  1033. fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
  1034. fl_gh->gh_error = 0;
  1035. msleep(sleeptime);
  1036. }
  1037. if (error) {
  1038. gfs2_holder_uninit(fl_gh);
  1039. if (error == GLR_TRYFAILED)
  1040. error = -EAGAIN;
  1041. } else {
  1042. error = locks_lock_file_wait(file, fl);
  1043. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1044. }
  1045. out:
  1046. mutex_unlock(&fp->f_fl_mutex);
  1047. return error;
  1048. }
  1049. static void do_unflock(struct file *file, struct file_lock *fl)
  1050. {
  1051. struct gfs2_file *fp = file->private_data;
  1052. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1053. mutex_lock(&fp->f_fl_mutex);
  1054. locks_lock_file_wait(file, fl);
  1055. if (gfs2_holder_initialized(fl_gh)) {
  1056. gfs2_glock_dq(fl_gh);
  1057. gfs2_holder_uninit(fl_gh);
  1058. }
  1059. mutex_unlock(&fp->f_fl_mutex);
  1060. }
  1061. /**
  1062. * gfs2_flock - acquire/release a flock lock on a file
  1063. * @file: the file pointer
  1064. * @cmd: either modify or retrieve lock state, possibly wait
  1065. * @fl: type and range of lock
  1066. *
  1067. * Returns: errno
  1068. */
  1069. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  1070. {
  1071. if (!(fl->fl_flags & FL_FLOCK))
  1072. return -ENOLCK;
  1073. if (fl->fl_type & LOCK_MAND)
  1074. return -EOPNOTSUPP;
  1075. if (fl->fl_type == F_UNLCK) {
  1076. do_unflock(file, fl);
  1077. return 0;
  1078. } else {
  1079. return do_flock(file, cmd, fl);
  1080. }
  1081. }
  1082. const struct file_operations gfs2_file_fops = {
  1083. .llseek = gfs2_llseek,
  1084. .read_iter = gfs2_file_read_iter,
  1085. .write_iter = gfs2_file_write_iter,
  1086. .unlocked_ioctl = gfs2_ioctl,
  1087. .mmap = gfs2_mmap,
  1088. .open = gfs2_open,
  1089. .release = gfs2_release,
  1090. .fsync = gfs2_fsync,
  1091. .lock = gfs2_lock,
  1092. .flock = gfs2_flock,
  1093. .splice_read = generic_file_splice_read,
  1094. .splice_write = gfs2_file_splice_write,
  1095. .setlease = simple_nosetlease,
  1096. .fallocate = gfs2_fallocate,
  1097. };
  1098. const struct file_operations gfs2_dir_fops = {
  1099. .iterate_shared = gfs2_readdir,
  1100. .unlocked_ioctl = gfs2_ioctl,
  1101. .open = gfs2_open,
  1102. .release = gfs2_release,
  1103. .fsync = gfs2_fsync,
  1104. .lock = gfs2_lock,
  1105. .flock = gfs2_flock,
  1106. .llseek = default_llseek,
  1107. };
  1108. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  1109. const struct file_operations gfs2_file_fops_nolock = {
  1110. .llseek = gfs2_llseek,
  1111. .read_iter = gfs2_file_read_iter,
  1112. .write_iter = gfs2_file_write_iter,
  1113. .unlocked_ioctl = gfs2_ioctl,
  1114. .mmap = gfs2_mmap,
  1115. .open = gfs2_open,
  1116. .release = gfs2_release,
  1117. .fsync = gfs2_fsync,
  1118. .splice_read = generic_file_splice_read,
  1119. .splice_write = gfs2_file_splice_write,
  1120. .setlease = generic_setlease,
  1121. .fallocate = gfs2_fallocate,
  1122. };
  1123. const struct file_operations gfs2_dir_fops_nolock = {
  1124. .iterate_shared = gfs2_readdir,
  1125. .unlocked_ioctl = gfs2_ioctl,
  1126. .open = gfs2_open,
  1127. .release = gfs2_release,
  1128. .fsync = gfs2_fsync,
  1129. .llseek = default_llseek,
  1130. };