file.c 19 KB

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