ops_file.c 17 KB

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