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