ops_file.c 19 KB

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