ops_file.c 19 KB

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