file.c 18 KB

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