ops_file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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/ext2_fs.h>
  21. #include <linux/crc32.h>
  22. #include <linux/writeback.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/dlm.h>
  25. #include <linux/dlm_plock.h>
  26. #include "gfs2.h"
  27. #include "incore.h"
  28. #include "bmap.h"
  29. #include "dir.h"
  30. #include "glock.h"
  31. #include "glops.h"
  32. #include "inode.h"
  33. #include "log.h"
  34. #include "meta_io.h"
  35. #include "quota.h"
  36. #include "rgrp.h"
  37. #include "trans.h"
  38. #include "util.h"
  39. #include "eaops.h"
  40. #include "ops_address.h"
  41. /**
  42. * gfs2_llseek - seek to a location in a file
  43. * @file: the file
  44. * @offset: the offset
  45. * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  46. *
  47. * SEEK_END requires the glock for the file because it references the
  48. * file's size.
  49. *
  50. * Returns: The new offset, or errno
  51. */
  52. static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
  53. {
  54. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  55. struct gfs2_holder i_gh;
  56. loff_t error;
  57. if (origin == 2) {
  58. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  59. &i_gh);
  60. if (!error) {
  61. error = generic_file_llseek_unlocked(file, offset, origin);
  62. gfs2_glock_dq_uninit(&i_gh);
  63. }
  64. } else
  65. error = generic_file_llseek_unlocked(file, offset, origin);
  66. return error;
  67. }
  68. /**
  69. * gfs2_readdir - Read directory entries from a directory
  70. * @file: The directory to read from
  71. * @dirent: Buffer for dirents
  72. * @filldir: Function used to do the copying
  73. *
  74. * Returns: errno
  75. */
  76. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  77. {
  78. struct inode *dir = file->f_mapping->host;
  79. struct gfs2_inode *dip = GFS2_I(dir);
  80. struct gfs2_holder d_gh;
  81. u64 offset = file->f_pos;
  82. int error;
  83. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  84. error = gfs2_glock_nq(&d_gh);
  85. if (error) {
  86. gfs2_holder_uninit(&d_gh);
  87. return error;
  88. }
  89. error = gfs2_dir_read(dir, &offset, dirent, filldir);
  90. gfs2_glock_dq_uninit(&d_gh);
  91. file->f_pos = offset;
  92. return error;
  93. }
  94. /**
  95. * fsflags_cvt
  96. * @table: A table of 32 u32 flags
  97. * @val: a 32 bit value to convert
  98. *
  99. * This function can be used to convert between fsflags values and
  100. * GFS2's own flags values.
  101. *
  102. * Returns: the converted flags
  103. */
  104. static u32 fsflags_cvt(const u32 *table, u32 val)
  105. {
  106. u32 res = 0;
  107. while(val) {
  108. if (val & 1)
  109. res |= *table;
  110. table++;
  111. val >>= 1;
  112. }
  113. return res;
  114. }
  115. static const u32 fsflags_to_gfs2[32] = {
  116. [3] = GFS2_DIF_SYNC,
  117. [4] = GFS2_DIF_IMMUTABLE,
  118. [5] = GFS2_DIF_APPENDONLY,
  119. [7] = GFS2_DIF_NOATIME,
  120. [12] = GFS2_DIF_EXHASH,
  121. [14] = GFS2_DIF_INHERIT_JDATA,
  122. };
  123. static const u32 gfs2_to_fsflags[32] = {
  124. [gfs2fl_Sync] = FS_SYNC_FL,
  125. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  126. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  127. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  128. [gfs2fl_ExHash] = FS_INDEX_FL,
  129. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  130. };
  131. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  132. {
  133. struct inode *inode = filp->f_path.dentry->d_inode;
  134. struct gfs2_inode *ip = GFS2_I(inode);
  135. struct gfs2_holder gh;
  136. int error;
  137. u32 fsflags;
  138. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  139. error = gfs2_glock_nq(&gh);
  140. if (error)
  141. return error;
  142. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_diskflags);
  143. if (!S_ISDIR(inode->i_mode) && ip->i_diskflags & GFS2_DIF_JDATA)
  144. fsflags |= FS_JOURNAL_DATA_FL;
  145. if (put_user(fsflags, ptr))
  146. error = -EFAULT;
  147. gfs2_glock_dq(&gh);
  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);
  156. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  157. flags |= S_IMMUTABLE;
  158. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  159. flags |= S_APPEND;
  160. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  161. flags |= S_NOATIME;
  162. if (ip->i_diskflags & GFS2_DIF_SYNC)
  163. flags |= S_SYNC;
  164. inode->i_flags = flags;
  165. }
  166. /* Flags that can be set by user space */
  167. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  168. GFS2_DIF_IMMUTABLE| \
  169. GFS2_DIF_APPENDONLY| \
  170. GFS2_DIF_NOATIME| \
  171. GFS2_DIF_SYNC| \
  172. GFS2_DIF_SYSTEM| \
  173. GFS2_DIF_INHERIT_JDATA)
  174. /**
  175. * gfs2_set_flags - set flags on an inode
  176. * @inode: The inode
  177. * @flags: The flags to set
  178. * @mask: Indicates which flags are valid
  179. *
  180. */
  181. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  182. {
  183. struct inode *inode = filp->f_path.dentry->d_inode;
  184. struct gfs2_inode *ip = GFS2_I(inode);
  185. struct gfs2_sbd *sdp = GFS2_SB(inode);
  186. struct buffer_head *bh;
  187. struct gfs2_holder gh;
  188. int error;
  189. u32 new_flags, flags;
  190. error = mnt_want_write(filp->f_path.mnt);
  191. if (error)
  192. return error;
  193. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  194. if (error)
  195. goto out_drop_write;
  196. flags = ip->i_diskflags;
  197. new_flags = (flags & ~mask) | (reqflags & mask);
  198. if ((new_flags ^ flags) == 0)
  199. goto out;
  200. error = -EINVAL;
  201. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  202. goto out;
  203. error = -EPERM;
  204. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  205. goto out;
  206. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  207. goto out;
  208. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  209. !capable(CAP_LINUX_IMMUTABLE))
  210. goto out;
  211. if (!IS_IMMUTABLE(inode)) {
  212. error = gfs2_permission(inode, MAY_WRITE);
  213. if (error)
  214. goto out;
  215. }
  216. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  217. if (flags & GFS2_DIF_JDATA)
  218. gfs2_log_flush(sdp, ip->i_gl);
  219. error = filemap_fdatawrite(inode->i_mapping);
  220. if (error)
  221. goto out;
  222. error = filemap_fdatawait(inode->i_mapping);
  223. if (error)
  224. goto out;
  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. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  233. ip->i_diskflags = new_flags;
  234. gfs2_dinode_out(ip, bh->b_data);
  235. brelse(bh);
  236. gfs2_set_inode_flags(inode);
  237. gfs2_set_aops(inode);
  238. out_trans_end:
  239. gfs2_trans_end(sdp);
  240. out:
  241. gfs2_glock_dq_uninit(&gh);
  242. out_drop_write:
  243. mnt_drop_write(filp->f_path.mnt);
  244. return error;
  245. }
  246. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  247. {
  248. struct inode *inode = filp->f_path.dentry->d_inode;
  249. u32 fsflags, gfsflags;
  250. if (get_user(fsflags, ptr))
  251. return -EFAULT;
  252. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  253. if (!S_ISDIR(inode->i_mode)) {
  254. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  255. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  256. return do_gfs2_set_flags(filp, gfsflags, ~0);
  257. }
  258. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  259. }
  260. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  261. {
  262. switch(cmd) {
  263. case FS_IOC_GETFLAGS:
  264. return gfs2_get_flags(filp, (u32 __user *)arg);
  265. case FS_IOC_SETFLAGS:
  266. return gfs2_set_flags(filp, (u32 __user *)arg);
  267. }
  268. return -ENOTTY;
  269. }
  270. /**
  271. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  272. * @page: The (locked) page to allocate backing for
  273. *
  274. * We try to allocate all the blocks required for the page in
  275. * one go. This might fail for various reasons, so we keep
  276. * trying until all the blocks to back this page are allocated.
  277. * If some of the blocks are already allocated, thats ok too.
  278. */
  279. static int gfs2_allocate_page_backing(struct page *page)
  280. {
  281. struct inode *inode = page->mapping->host;
  282. struct buffer_head bh;
  283. unsigned long size = PAGE_CACHE_SIZE;
  284. u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  285. do {
  286. bh.b_state = 0;
  287. bh.b_size = size;
  288. gfs2_block_map(inode, lblock, &bh, 1);
  289. if (!buffer_mapped(&bh))
  290. return -EIO;
  291. size -= bh.b_size;
  292. lblock += (bh.b_size >> inode->i_blkbits);
  293. } while(size > 0);
  294. return 0;
  295. }
  296. /**
  297. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  298. * @vma: The virtual memory area
  299. * @page: The page which is about to become writable
  300. *
  301. * When the page becomes writable, we need to ensure that we have
  302. * blocks allocated on disk to back that page.
  303. */
  304. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  305. {
  306. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  307. struct gfs2_inode *ip = GFS2_I(inode);
  308. struct gfs2_sbd *sdp = GFS2_SB(inode);
  309. unsigned long last_index;
  310. u64 pos = page->index << PAGE_CACHE_SHIFT;
  311. unsigned int data_blocks, ind_blocks, rblocks;
  312. int alloc_required = 0;
  313. struct gfs2_holder gh;
  314. struct gfs2_alloc *al;
  315. int ret;
  316. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  317. ret = gfs2_glock_nq(&gh);
  318. if (ret)
  319. goto out;
  320. set_bit(GIF_SW_PAGED, &ip->i_flags);
  321. ret = gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE, &alloc_required);
  322. if (ret || !alloc_required)
  323. goto out_unlock;
  324. ret = -ENOMEM;
  325. al = gfs2_alloc_get(ip);
  326. if (al == NULL)
  327. goto out_unlock;
  328. ret = gfs2_quota_lock_check(ip);
  329. if (ret)
  330. goto out_alloc_put;
  331. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  332. al->al_requested = data_blocks + ind_blocks;
  333. ret = gfs2_inplace_reserve(ip);
  334. if (ret)
  335. goto out_quota_unlock;
  336. rblocks = RES_DINODE + ind_blocks;
  337. if (gfs2_is_jdata(ip))
  338. rblocks += data_blocks ? data_blocks : 1;
  339. if (ind_blocks || data_blocks)
  340. rblocks += RES_STATFS + RES_QUOTA;
  341. ret = gfs2_trans_begin(sdp, rblocks, 0);
  342. if (ret)
  343. goto out_trans_fail;
  344. lock_page(page);
  345. ret = -EINVAL;
  346. last_index = ip->i_inode.i_size >> PAGE_CACHE_SHIFT;
  347. if (page->index > last_index)
  348. goto out_unlock_page;
  349. ret = 0;
  350. if (!PageUptodate(page) || page->mapping != ip->i_inode.i_mapping)
  351. goto out_unlock_page;
  352. if (gfs2_is_stuffed(ip)) {
  353. ret = gfs2_unstuff_dinode(ip, page);
  354. if (ret)
  355. goto out_unlock_page;
  356. }
  357. ret = gfs2_allocate_page_backing(page);
  358. out_unlock_page:
  359. unlock_page(page);
  360. gfs2_trans_end(sdp);
  361. out_trans_fail:
  362. gfs2_inplace_release(ip);
  363. out_quota_unlock:
  364. gfs2_quota_unlock(ip);
  365. out_alloc_put:
  366. gfs2_alloc_put(ip);
  367. out_unlock:
  368. gfs2_glock_dq(&gh);
  369. out:
  370. gfs2_holder_uninit(&gh);
  371. return ret;
  372. }
  373. static struct vm_operations_struct gfs2_vm_ops = {
  374. .fault = filemap_fault,
  375. .page_mkwrite = gfs2_page_mkwrite,
  376. };
  377. /**
  378. * gfs2_mmap -
  379. * @file: The file to map
  380. * @vma: The VMA which described the mapping
  381. *
  382. * Returns: 0 or error code
  383. */
  384. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  385. {
  386. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  387. struct gfs2_holder i_gh;
  388. int error;
  389. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  390. error = gfs2_glock_nq(&i_gh);
  391. if (error) {
  392. gfs2_holder_uninit(&i_gh);
  393. return error;
  394. }
  395. vma->vm_ops = &gfs2_vm_ops;
  396. gfs2_glock_dq_uninit(&i_gh);
  397. return error;
  398. }
  399. /**
  400. * gfs2_open - open a file
  401. * @inode: the inode to open
  402. * @file: the struct file for this opening
  403. *
  404. * Returns: errno
  405. */
  406. static int gfs2_open(struct inode *inode, struct file *file)
  407. {
  408. struct gfs2_inode *ip = GFS2_I(inode);
  409. struct gfs2_holder i_gh;
  410. struct gfs2_file *fp;
  411. int error;
  412. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  413. if (!fp)
  414. return -ENOMEM;
  415. mutex_init(&fp->f_fl_mutex);
  416. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  417. file->private_data = fp;
  418. if (S_ISREG(ip->i_inode.i_mode)) {
  419. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  420. &i_gh);
  421. if (error)
  422. goto fail;
  423. if (!(file->f_flags & O_LARGEFILE) &&
  424. ip->i_disksize > MAX_NON_LFS) {
  425. error = -EOVERFLOW;
  426. goto fail_gunlock;
  427. }
  428. gfs2_glock_dq_uninit(&i_gh);
  429. }
  430. return 0;
  431. fail_gunlock:
  432. gfs2_glock_dq_uninit(&i_gh);
  433. fail:
  434. file->private_data = NULL;
  435. kfree(fp);
  436. return error;
  437. }
  438. /**
  439. * gfs2_close - called to close a struct file
  440. * @inode: the inode the struct file belongs to
  441. * @file: the struct file being closed
  442. *
  443. * Returns: errno
  444. */
  445. static int gfs2_close(struct inode *inode, struct file *file)
  446. {
  447. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  448. struct gfs2_file *fp;
  449. fp = file->private_data;
  450. file->private_data = NULL;
  451. if (gfs2_assert_warn(sdp, fp))
  452. return -EIO;
  453. kfree(fp);
  454. return 0;
  455. }
  456. /**
  457. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  458. * @file: the file that points to the dentry (we ignore this)
  459. * @dentry: the dentry that points to the inode to sync
  460. *
  461. * The VFS will flush "normal" data for us. We only need to worry
  462. * about metadata here. For journaled data, we just do a log flush
  463. * as we can't avoid it. Otherwise we can just bale out if datasync
  464. * is set. For stuffed inodes we must flush the log in order to
  465. * ensure that all data is on disk.
  466. *
  467. * The call to write_inode_now() is there to write back metadata and
  468. * the inode itself. It does also try and write the data, but thats
  469. * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
  470. * for us.
  471. *
  472. * Returns: errno
  473. */
  474. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  475. {
  476. struct inode *inode = dentry->d_inode;
  477. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  478. int ret = 0;
  479. if (gfs2_is_jdata(GFS2_I(inode))) {
  480. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  481. return 0;
  482. }
  483. if (sync_state != 0) {
  484. if (!datasync)
  485. ret = write_inode_now(inode, 0);
  486. if (gfs2_is_stuffed(GFS2_I(inode)))
  487. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  488. }
  489. return ret;
  490. }
  491. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  492. /**
  493. * gfs2_setlease - acquire/release a file lease
  494. * @file: the file pointer
  495. * @arg: lease type
  496. * @fl: file lock
  497. *
  498. * We don't currently have a way to enforce a lease across the whole
  499. * cluster; until we do, disable leases (by just returning -EINVAL),
  500. * unless the administrator has requested purely local locking.
  501. *
  502. * Returns: errno
  503. */
  504. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  505. {
  506. return -EINVAL;
  507. }
  508. /**
  509. * gfs2_lock - acquire/release a posix lock on a file
  510. * @file: the file pointer
  511. * @cmd: either modify or retrieve lock state, possibly wait
  512. * @fl: type and range of lock
  513. *
  514. * Returns: errno
  515. */
  516. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  517. {
  518. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  519. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  520. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  521. if (!(fl->fl_flags & FL_POSIX))
  522. return -ENOLCK;
  523. if (__mandatory_lock(&ip->i_inode))
  524. return -ENOLCK;
  525. if (cmd == F_CANCELLK) {
  526. /* Hack: */
  527. cmd = F_SETLK;
  528. fl->fl_type = F_UNLCK;
  529. }
  530. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  531. return -EIO;
  532. if (IS_GETLK(cmd))
  533. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  534. else if (fl->fl_type == F_UNLCK)
  535. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  536. else
  537. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  538. }
  539. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  540. {
  541. struct gfs2_file *fp = file->private_data;
  542. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  543. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  544. struct gfs2_glock *gl;
  545. unsigned int state;
  546. int flags;
  547. int error = 0;
  548. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  549. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  550. mutex_lock(&fp->f_fl_mutex);
  551. gl = fl_gh->gh_gl;
  552. if (gl) {
  553. if (fl_gh->gh_state == state)
  554. goto out;
  555. flock_lock_file_wait(file,
  556. &(struct file_lock){.fl_type = F_UNLCK});
  557. gfs2_glock_dq_wait(fl_gh);
  558. gfs2_holder_reinit(state, flags, fl_gh);
  559. } else {
  560. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  561. &gfs2_flock_glops, CREATE, &gl);
  562. if (error)
  563. goto out;
  564. gfs2_holder_init(gl, state, flags, fl_gh);
  565. gfs2_glock_put(gl);
  566. }
  567. error = gfs2_glock_nq(fl_gh);
  568. if (error) {
  569. gfs2_holder_uninit(fl_gh);
  570. if (error == GLR_TRYFAILED)
  571. error = -EAGAIN;
  572. } else {
  573. error = flock_lock_file_wait(file, fl);
  574. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  575. }
  576. out:
  577. mutex_unlock(&fp->f_fl_mutex);
  578. return error;
  579. }
  580. static void do_unflock(struct file *file, struct file_lock *fl)
  581. {
  582. struct gfs2_file *fp = file->private_data;
  583. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  584. mutex_lock(&fp->f_fl_mutex);
  585. flock_lock_file_wait(file, fl);
  586. if (fl_gh->gh_gl)
  587. gfs2_glock_dq_uninit(fl_gh);
  588. mutex_unlock(&fp->f_fl_mutex);
  589. }
  590. /**
  591. * gfs2_flock - acquire/release a flock lock on a file
  592. * @file: the file pointer
  593. * @cmd: either modify or retrieve lock state, possibly wait
  594. * @fl: type and range of lock
  595. *
  596. * Returns: errno
  597. */
  598. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  599. {
  600. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  601. if (!(fl->fl_flags & FL_FLOCK))
  602. return -ENOLCK;
  603. if (__mandatory_lock(&ip->i_inode))
  604. return -ENOLCK;
  605. if (fl->fl_type == F_UNLCK) {
  606. do_unflock(file, fl);
  607. return 0;
  608. } else {
  609. return do_flock(file, cmd, fl);
  610. }
  611. }
  612. const struct file_operations *gfs2_file_fops = &(const struct file_operations){
  613. .llseek = gfs2_llseek,
  614. .read = do_sync_read,
  615. .aio_read = generic_file_aio_read,
  616. .write = do_sync_write,
  617. .aio_write = generic_file_aio_write,
  618. .unlocked_ioctl = gfs2_ioctl,
  619. .mmap = gfs2_mmap,
  620. .open = gfs2_open,
  621. .release = gfs2_close,
  622. .fsync = gfs2_fsync,
  623. .lock = gfs2_lock,
  624. .flock = gfs2_flock,
  625. .splice_read = generic_file_splice_read,
  626. .splice_write = generic_file_splice_write,
  627. .setlease = gfs2_setlease,
  628. };
  629. const struct file_operations *gfs2_dir_fops = &(const struct file_operations){
  630. .readdir = gfs2_readdir,
  631. .unlocked_ioctl = gfs2_ioctl,
  632. .open = gfs2_open,
  633. .release = gfs2_close,
  634. .fsync = gfs2_fsync,
  635. .lock = gfs2_lock,
  636. .flock = gfs2_flock,
  637. };
  638. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  639. const struct file_operations *gfs2_file_fops_nolock = &(const struct file_operations){
  640. .llseek = gfs2_llseek,
  641. .read = do_sync_read,
  642. .aio_read = generic_file_aio_read,
  643. .write = do_sync_write,
  644. .aio_write = generic_file_aio_write,
  645. .unlocked_ioctl = gfs2_ioctl,
  646. .mmap = gfs2_mmap,
  647. .open = gfs2_open,
  648. .release = gfs2_close,
  649. .fsync = gfs2_fsync,
  650. .splice_read = generic_file_splice_read,
  651. .splice_write = generic_file_splice_write,
  652. .setlease = generic_setlease,
  653. };
  654. const struct file_operations *gfs2_dir_fops_nolock = &(const struct file_operations){
  655. .readdir = gfs2_readdir,
  656. .unlocked_ioctl = gfs2_ioctl,
  657. .open = gfs2_open,
  658. .release = gfs2_close,
  659. .fsync = gfs2_fsync,
  660. };