ops_file.c 19 KB

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