ops_file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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/fs.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/ext2_fs.h>
  20. #include <linux/crc32.h>
  21. #include <linux/lm_interface.h>
  22. #include <linux/writeback.h>
  23. #include <asm/uaccess.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "glock.h"
  29. #include "glops.h"
  30. #include "inode.h"
  31. #include "lm.h"
  32. #include "log.h"
  33. #include "meta_io.h"
  34. #include "ops_file.h"
  35. #include "ops_vm.h"
  36. #include "quota.h"
  37. #include "rgrp.h"
  38. #include "trans.h"
  39. #include "util.h"
  40. #include "eaops.h"
  41. /*
  42. * Most fields left uninitialised to catch anybody who tries to
  43. * use them. f_flags set to prevent file_accessed() from touching
  44. * any other part of this. Its use is purely as a flag so that we
  45. * know (in readpage()) whether or not do to locking.
  46. */
  47. struct file gfs2_internal_file_sentinel = {
  48. .f_flags = O_NOATIME|O_RDONLY,
  49. };
  50. static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
  51. unsigned long offset, unsigned long size)
  52. {
  53. char *kaddr;
  54. unsigned long count = desc->count;
  55. if (size > count)
  56. size = count;
  57. kaddr = kmap(page);
  58. memcpy(desc->arg.data, kaddr + offset, size);
  59. kunmap(page);
  60. desc->count = count - size;
  61. desc->written += size;
  62. desc->arg.buf += size;
  63. return size;
  64. }
  65. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  66. char *buf, loff_t *pos, unsigned size)
  67. {
  68. struct inode *inode = &ip->i_inode;
  69. read_descriptor_t desc;
  70. desc.written = 0;
  71. desc.arg.data = buf;
  72. desc.count = size;
  73. desc.error = 0;
  74. do_generic_mapping_read(inode->i_mapping, ra_state,
  75. &gfs2_internal_file_sentinel, pos, &desc,
  76. gfs2_read_actor);
  77. return desc.written ? desc.written : desc.error;
  78. }
  79. /**
  80. * gfs2_llseek - seek to a location in a file
  81. * @file: the file
  82. * @offset: the offset
  83. * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  84. *
  85. * SEEK_END requires the glock for the file because it references the
  86. * file's size.
  87. *
  88. * Returns: The new offset, or errno
  89. */
  90. static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
  91. {
  92. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  93. struct gfs2_holder i_gh;
  94. loff_t error;
  95. if (origin == 2) {
  96. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  97. &i_gh);
  98. if (!error) {
  99. error = remote_llseek(file, offset, origin);
  100. gfs2_glock_dq_uninit(&i_gh);
  101. }
  102. } else
  103. error = remote_llseek(file, offset, origin);
  104. return error;
  105. }
  106. /**
  107. * gfs2_readdir - Read directory entries from a directory
  108. * @file: The directory to read from
  109. * @dirent: Buffer for dirents
  110. * @filldir: Function used to do the copying
  111. *
  112. * Returns: errno
  113. */
  114. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  115. {
  116. struct inode *dir = file->f_mapping->host;
  117. struct gfs2_inode *dip = GFS2_I(dir);
  118. struct gfs2_holder d_gh;
  119. u64 offset = file->f_pos;
  120. int error;
  121. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
  122. error = gfs2_glock_nq_atime(&d_gh);
  123. if (error) {
  124. gfs2_holder_uninit(&d_gh);
  125. return error;
  126. }
  127. error = gfs2_dir_read(dir, &offset, dirent, filldir);
  128. gfs2_glock_dq_uninit(&d_gh);
  129. file->f_pos = offset;
  130. return error;
  131. }
  132. /**
  133. * fsflags_cvt
  134. * @table: A table of 32 u32 flags
  135. * @val: a 32 bit value to convert
  136. *
  137. * This function can be used to convert between fsflags values and
  138. * GFS2's own flags values.
  139. *
  140. * Returns: the converted flags
  141. */
  142. static u32 fsflags_cvt(const u32 *table, u32 val)
  143. {
  144. u32 res = 0;
  145. while(val) {
  146. if (val & 1)
  147. res |= *table;
  148. table++;
  149. val >>= 1;
  150. }
  151. return res;
  152. }
  153. static const u32 fsflags_to_gfs2[32] = {
  154. [3] = GFS2_DIF_SYNC,
  155. [4] = GFS2_DIF_IMMUTABLE,
  156. [5] = GFS2_DIF_APPENDONLY,
  157. [7] = GFS2_DIF_NOATIME,
  158. [12] = GFS2_DIF_EXHASH,
  159. [14] = GFS2_DIF_INHERIT_JDATA,
  160. [20] = GFS2_DIF_INHERIT_DIRECTIO,
  161. };
  162. static const u32 gfs2_to_fsflags[32] = {
  163. [gfs2fl_Sync] = FS_SYNC_FL,
  164. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  165. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  166. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  167. [gfs2fl_ExHash] = FS_INDEX_FL,
  168. [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
  169. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  170. };
  171. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  172. {
  173. struct inode *inode = filp->f_path.dentry->d_inode;
  174. struct gfs2_inode *ip = GFS2_I(inode);
  175. struct gfs2_holder gh;
  176. int error;
  177. u32 fsflags;
  178. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  179. error = gfs2_glock_nq_atime(&gh);
  180. if (error)
  181. return error;
  182. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
  183. if (!S_ISDIR(inode->i_mode)) {
  184. if (ip->i_di.di_flags & GFS2_DIF_JDATA)
  185. fsflags |= FS_JOURNAL_DATA_FL;
  186. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  187. fsflags |= FS_DIRECTIO_FL;
  188. }
  189. if (put_user(fsflags, ptr))
  190. error = -EFAULT;
  191. gfs2_glock_dq_m(1, &gh);
  192. gfs2_holder_uninit(&gh);
  193. return error;
  194. }
  195. void gfs2_set_inode_flags(struct inode *inode)
  196. {
  197. struct gfs2_inode *ip = GFS2_I(inode);
  198. struct gfs2_dinode_host *di = &ip->i_di;
  199. unsigned int flags = inode->i_flags;
  200. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  201. if (di->di_flags & GFS2_DIF_IMMUTABLE)
  202. flags |= S_IMMUTABLE;
  203. if (di->di_flags & GFS2_DIF_APPENDONLY)
  204. flags |= S_APPEND;
  205. if (di->di_flags & GFS2_DIF_NOATIME)
  206. flags |= S_NOATIME;
  207. if (di->di_flags & GFS2_DIF_SYNC)
  208. flags |= S_SYNC;
  209. inode->i_flags = flags;
  210. }
  211. /* Flags that can be set by user space */
  212. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  213. GFS2_DIF_DIRECTIO| \
  214. GFS2_DIF_IMMUTABLE| \
  215. GFS2_DIF_APPENDONLY| \
  216. GFS2_DIF_NOATIME| \
  217. GFS2_DIF_SYNC| \
  218. GFS2_DIF_SYSTEM| \
  219. GFS2_DIF_INHERIT_DIRECTIO| \
  220. GFS2_DIF_INHERIT_JDATA)
  221. /**
  222. * gfs2_set_flags - set flags on an inode
  223. * @inode: The inode
  224. * @flags: The flags to set
  225. * @mask: Indicates which flags are valid
  226. *
  227. */
  228. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  229. {
  230. struct inode *inode = filp->f_path.dentry->d_inode;
  231. struct gfs2_inode *ip = GFS2_I(inode);
  232. struct gfs2_sbd *sdp = GFS2_SB(inode);
  233. struct buffer_head *bh;
  234. struct gfs2_holder gh;
  235. int error;
  236. u32 new_flags, flags;
  237. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  238. if (error)
  239. return error;
  240. flags = ip->i_di.di_flags;
  241. new_flags = (flags & ~mask) | (reqflags & mask);
  242. if ((new_flags ^ flags) == 0)
  243. goto out;
  244. error = -EINVAL;
  245. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  246. goto out;
  247. error = -EPERM;
  248. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  249. goto out;
  250. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  251. goto out;
  252. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  253. !capable(CAP_LINUX_IMMUTABLE))
  254. goto out;
  255. if (!IS_IMMUTABLE(inode)) {
  256. error = permission(inode, MAY_WRITE, NULL);
  257. if (error)
  258. goto out;
  259. }
  260. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  261. if (error)
  262. goto out;
  263. error = gfs2_meta_inode_buffer(ip, &bh);
  264. if (error)
  265. goto out_trans_end;
  266. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  267. ip->i_di.di_flags = new_flags;
  268. gfs2_dinode_out(ip, bh->b_data);
  269. brelse(bh);
  270. gfs2_set_inode_flags(inode);
  271. out_trans_end:
  272. gfs2_trans_end(sdp);
  273. out:
  274. gfs2_glock_dq_uninit(&gh);
  275. return error;
  276. }
  277. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  278. {
  279. struct inode *inode = filp->f_path.dentry->d_inode;
  280. u32 fsflags, gfsflags;
  281. if (get_user(fsflags, ptr))
  282. return -EFAULT;
  283. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  284. if (!S_ISDIR(inode->i_mode)) {
  285. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  286. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  287. if (gfsflags & GFS2_DIF_INHERIT_DIRECTIO)
  288. gfsflags ^= (GFS2_DIF_DIRECTIO | GFS2_DIF_INHERIT_DIRECTIO);
  289. return do_gfs2_set_flags(filp, gfsflags, ~0);
  290. }
  291. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  292. }
  293. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  294. {
  295. switch(cmd) {
  296. case FS_IOC_GETFLAGS:
  297. return gfs2_get_flags(filp, (u32 __user *)arg);
  298. case FS_IOC_SETFLAGS:
  299. return gfs2_set_flags(filp, (u32 __user *)arg);
  300. }
  301. return -ENOTTY;
  302. }
  303. /**
  304. * gfs2_mmap -
  305. * @file: The file to map
  306. * @vma: The VMA which described the mapping
  307. *
  308. * Returns: 0 or error code
  309. */
  310. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  311. {
  312. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  313. struct gfs2_holder i_gh;
  314. int error;
  315. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  316. error = gfs2_glock_nq_atime(&i_gh);
  317. if (error) {
  318. gfs2_holder_uninit(&i_gh);
  319. return error;
  320. }
  321. /* This is VM_MAYWRITE instead of VM_WRITE because a call
  322. to mprotect() can turn on VM_WRITE later. */
  323. if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
  324. (VM_MAYSHARE | VM_MAYWRITE))
  325. vma->vm_ops = &gfs2_vm_ops_sharewrite;
  326. else
  327. vma->vm_ops = &gfs2_vm_ops_private;
  328. gfs2_glock_dq_uninit(&i_gh);
  329. return error;
  330. }
  331. /**
  332. * gfs2_open - open a file
  333. * @inode: the inode to open
  334. * @file: the struct file for this opening
  335. *
  336. * Returns: errno
  337. */
  338. static int gfs2_open(struct inode *inode, struct file *file)
  339. {
  340. struct gfs2_inode *ip = GFS2_I(inode);
  341. struct gfs2_holder i_gh;
  342. struct gfs2_file *fp;
  343. int error;
  344. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  345. if (!fp)
  346. return -ENOMEM;
  347. mutex_init(&fp->f_fl_mutex);
  348. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  349. file->private_data = fp;
  350. if (S_ISREG(ip->i_inode.i_mode)) {
  351. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  352. &i_gh);
  353. if (error)
  354. goto fail;
  355. if (!(file->f_flags & O_LARGEFILE) &&
  356. ip->i_di.di_size > MAX_NON_LFS) {
  357. error = -EOVERFLOW;
  358. goto fail_gunlock;
  359. }
  360. /* Listen to the Direct I/O flag */
  361. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  362. file->f_flags |= O_DIRECT;
  363. gfs2_glock_dq_uninit(&i_gh);
  364. }
  365. return 0;
  366. fail_gunlock:
  367. gfs2_glock_dq_uninit(&i_gh);
  368. fail:
  369. file->private_data = NULL;
  370. kfree(fp);
  371. return error;
  372. }
  373. /**
  374. * gfs2_close - called to close a struct file
  375. * @inode: the inode the struct file belongs to
  376. * @file: the struct file being closed
  377. *
  378. * Returns: errno
  379. */
  380. static int gfs2_close(struct inode *inode, struct file *file)
  381. {
  382. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  383. struct gfs2_file *fp;
  384. fp = file->private_data;
  385. file->private_data = NULL;
  386. if (gfs2_assert_warn(sdp, fp))
  387. return -EIO;
  388. kfree(fp);
  389. return 0;
  390. }
  391. /**
  392. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  393. * @file: the file that points to the dentry (we ignore this)
  394. * @dentry: the dentry that points to the inode to sync
  395. *
  396. * The VFS will flush "normal" data for us. We only need to worry
  397. * about metadata here. For journaled data, we just do a log flush
  398. * as we can't avoid it. Otherwise we can just bale out if datasync
  399. * is set. For stuffed inodes we must flush the log in order to
  400. * ensure that all data is on disk.
  401. *
  402. * The call to write_inode_now() is there to write back metadata and
  403. * the inode itself. It does also try and write the data, but thats
  404. * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
  405. * for us.
  406. *
  407. * Returns: errno
  408. */
  409. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  410. {
  411. struct inode *inode = dentry->d_inode;
  412. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  413. int ret = 0;
  414. if (gfs2_is_jdata(GFS2_I(inode))) {
  415. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  416. return 0;
  417. }
  418. if (sync_state != 0) {
  419. if (!datasync)
  420. ret = write_inode_now(inode, 0);
  421. if (gfs2_is_stuffed(GFS2_I(inode)))
  422. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  423. }
  424. return ret;
  425. }
  426. /**
  427. * gfs2_setlease - acquire/release a file lease
  428. * @file: the file pointer
  429. * @arg: lease type
  430. * @fl: file lock
  431. *
  432. * Returns: errno
  433. */
  434. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  435. {
  436. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  437. /*
  438. * We don't currently have a way to enforce a lease across the whole
  439. * cluster; until we do, disable leases (by just returning -EINVAL),
  440. * unless the administrator has requested purely local locking.
  441. */
  442. if (!sdp->sd_args.ar_localflocks)
  443. return -EINVAL;
  444. return generic_setlease(file, arg, fl);
  445. }
  446. /**
  447. * gfs2_lock - acquire/release a posix lock on a file
  448. * @file: the file pointer
  449. * @cmd: either modify or retrieve lock state, possibly wait
  450. * @fl: type and range of lock
  451. *
  452. * Returns: errno
  453. */
  454. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  455. {
  456. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  457. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  458. struct lm_lockname name =
  459. { .ln_number = ip->i_no_addr,
  460. .ln_type = LM_TYPE_PLOCK };
  461. if (!(fl->fl_flags & FL_POSIX))
  462. return -ENOLCK;
  463. if (__mandatory_lock(&ip->i_inode))
  464. return -ENOLCK;
  465. if (sdp->sd_args.ar_localflocks) {
  466. if (IS_GETLK(cmd)) {
  467. posix_test_lock(file, fl);
  468. return 0;
  469. } else {
  470. return posix_lock_file_wait(file, fl);
  471. }
  472. }
  473. if (cmd == F_CANCELLK) {
  474. /* Hack: */
  475. cmd = F_SETLK;
  476. fl->fl_type = F_UNLCK;
  477. }
  478. if (IS_GETLK(cmd))
  479. return gfs2_lm_plock_get(sdp, &name, file, fl);
  480. else if (fl->fl_type == F_UNLCK)
  481. return gfs2_lm_punlock(sdp, &name, file, fl);
  482. else
  483. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  484. }
  485. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  486. {
  487. struct gfs2_file *fp = file->private_data;
  488. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  489. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  490. struct gfs2_glock *gl;
  491. unsigned int state;
  492. int flags;
  493. int error = 0;
  494. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  495. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE
  496. | GL_FLOCK;
  497. mutex_lock(&fp->f_fl_mutex);
  498. gl = fl_gh->gh_gl;
  499. if (gl) {
  500. if (fl_gh->gh_state == state)
  501. goto out;
  502. flock_lock_file_wait(file,
  503. &(struct file_lock){.fl_type = F_UNLCK});
  504. gfs2_glock_dq_wait(fl_gh);
  505. gfs2_holder_reinit(state, flags, fl_gh);
  506. } else {
  507. error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
  508. ip->i_no_addr, &gfs2_flock_glops,
  509. CREATE, &gl);
  510. if (error)
  511. goto out;
  512. gfs2_holder_init(gl, state, flags, fl_gh);
  513. gfs2_glock_put(gl);
  514. }
  515. error = gfs2_glock_nq(fl_gh);
  516. if (error) {
  517. gfs2_holder_uninit(fl_gh);
  518. if (error == GLR_TRYFAILED)
  519. error = -EAGAIN;
  520. } else {
  521. error = flock_lock_file_wait(file, fl);
  522. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  523. }
  524. out:
  525. mutex_unlock(&fp->f_fl_mutex);
  526. return error;
  527. }
  528. static void do_unflock(struct file *file, struct file_lock *fl)
  529. {
  530. struct gfs2_file *fp = file->private_data;
  531. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  532. mutex_lock(&fp->f_fl_mutex);
  533. flock_lock_file_wait(file, fl);
  534. if (fl_gh->gh_gl)
  535. gfs2_glock_dq_uninit(fl_gh);
  536. mutex_unlock(&fp->f_fl_mutex);
  537. }
  538. /**
  539. * gfs2_flock - acquire/release a flock lock on a file
  540. * @file: the file pointer
  541. * @cmd: either modify or retrieve lock state, possibly wait
  542. * @fl: type and range of lock
  543. *
  544. * Returns: errno
  545. */
  546. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  547. {
  548. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  549. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  550. if (!(fl->fl_flags & FL_FLOCK))
  551. return -ENOLCK;
  552. if (__mandatory_lock(&ip->i_inode))
  553. return -ENOLCK;
  554. if (sdp->sd_args.ar_localflocks)
  555. return flock_lock_file_wait(file, fl);
  556. if (fl->fl_type == F_UNLCK) {
  557. do_unflock(file, fl);
  558. return 0;
  559. } else {
  560. return do_flock(file, cmd, fl);
  561. }
  562. }
  563. const struct file_operations gfs2_file_fops = {
  564. .llseek = gfs2_llseek,
  565. .read = do_sync_read,
  566. .aio_read = generic_file_aio_read,
  567. .write = do_sync_write,
  568. .aio_write = generic_file_aio_write,
  569. .unlocked_ioctl = gfs2_ioctl,
  570. .mmap = gfs2_mmap,
  571. .open = gfs2_open,
  572. .release = gfs2_close,
  573. .fsync = gfs2_fsync,
  574. .lock = gfs2_lock,
  575. .flock = gfs2_flock,
  576. .splice_read = generic_file_splice_read,
  577. .splice_write = generic_file_splice_write,
  578. .setlease = gfs2_setlease,
  579. };
  580. const struct file_operations gfs2_dir_fops = {
  581. .readdir = gfs2_readdir,
  582. .unlocked_ioctl = gfs2_ioctl,
  583. .open = gfs2_open,
  584. .release = gfs2_close,
  585. .fsync = gfs2_fsync,
  586. .lock = gfs2_lock,
  587. .flock = gfs2_flock,
  588. };