ops_file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 "lm.h"
  32. #include "log.h"
  33. #include "meta_io.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. #include "eaops.h"
  39. #include "ops_address.h"
  40. /**
  41. * gfs2_llseek - seek to a location in a file
  42. * @file: the file
  43. * @offset: the offset
  44. * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  45. *
  46. * SEEK_END requires the glock for the file because it references the
  47. * file's size.
  48. *
  49. * Returns: The new offset, or errno
  50. */
  51. static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
  52. {
  53. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  54. struct gfs2_holder i_gh;
  55. loff_t error;
  56. if (origin == 2) {
  57. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  58. &i_gh);
  59. if (!error) {
  60. error = 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(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  335. if (ret)
  336. goto out_alloc_put;
  337. ret = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  338. if (ret)
  339. goto out_quota_unlock;
  340. al->al_requested = data_blocks + ind_blocks;
  341. ret = gfs2_inplace_reserve(ip);
  342. if (ret)
  343. goto out_quota_unlock;
  344. rblocks = RES_DINODE + ind_blocks;
  345. if (gfs2_is_jdata(ip))
  346. rblocks += data_blocks ? data_blocks : 1;
  347. if (ind_blocks || data_blocks)
  348. rblocks += RES_STATFS + RES_QUOTA;
  349. ret = gfs2_trans_begin(sdp, rblocks, 0);
  350. if (ret)
  351. goto out_trans_fail;
  352. lock_page(page);
  353. ret = -EINVAL;
  354. last_index = ip->i_inode.i_size >> PAGE_CACHE_SHIFT;
  355. if (page->index > last_index)
  356. goto out_unlock_page;
  357. ret = 0;
  358. if (!PageUptodate(page) || page->mapping != ip->i_inode.i_mapping)
  359. goto out_unlock_page;
  360. if (gfs2_is_stuffed(ip)) {
  361. ret = gfs2_unstuff_dinode(ip, page);
  362. if (ret)
  363. goto out_unlock_page;
  364. }
  365. ret = gfs2_allocate_page_backing(page);
  366. out_unlock_page:
  367. unlock_page(page);
  368. gfs2_trans_end(sdp);
  369. out_trans_fail:
  370. gfs2_inplace_release(ip);
  371. out_quota_unlock:
  372. gfs2_quota_unlock(ip);
  373. out_alloc_put:
  374. gfs2_alloc_put(ip);
  375. out_unlock:
  376. gfs2_glock_dq(&gh);
  377. out:
  378. gfs2_holder_uninit(&gh);
  379. return ret;
  380. }
  381. static 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. * Returns: 0 or error code
  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. struct gfs2_holder i_gh;
  396. int error;
  397. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  398. error = gfs2_glock_nq_atime(&i_gh);
  399. if (error) {
  400. gfs2_holder_uninit(&i_gh);
  401. return error;
  402. }
  403. vma->vm_ops = &gfs2_vm_ops;
  404. gfs2_glock_dq_uninit(&i_gh);
  405. return error;
  406. }
  407. /**
  408. * gfs2_open - open a file
  409. * @inode: the inode to open
  410. * @file: the struct file for this opening
  411. *
  412. * Returns: errno
  413. */
  414. static int gfs2_open(struct inode *inode, struct file *file)
  415. {
  416. struct gfs2_inode *ip = GFS2_I(inode);
  417. struct gfs2_holder i_gh;
  418. struct gfs2_file *fp;
  419. int error;
  420. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  421. if (!fp)
  422. return -ENOMEM;
  423. mutex_init(&fp->f_fl_mutex);
  424. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  425. file->private_data = fp;
  426. if (S_ISREG(ip->i_inode.i_mode)) {
  427. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  428. &i_gh);
  429. if (error)
  430. goto fail;
  431. if (!(file->f_flags & O_LARGEFILE) &&
  432. ip->i_di.di_size > MAX_NON_LFS) {
  433. error = -EOVERFLOW;
  434. goto fail_gunlock;
  435. }
  436. /* Listen to the Direct I/O flag */
  437. if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
  438. file->f_flags |= O_DIRECT;
  439. gfs2_glock_dq_uninit(&i_gh);
  440. }
  441. return 0;
  442. fail_gunlock:
  443. gfs2_glock_dq_uninit(&i_gh);
  444. fail:
  445. file->private_data = NULL;
  446. kfree(fp);
  447. return error;
  448. }
  449. /**
  450. * gfs2_close - called to close a struct file
  451. * @inode: the inode the struct file belongs to
  452. * @file: the struct file being closed
  453. *
  454. * Returns: errno
  455. */
  456. static int gfs2_close(struct inode *inode, struct file *file)
  457. {
  458. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  459. struct gfs2_file *fp;
  460. fp = file->private_data;
  461. file->private_data = NULL;
  462. if (gfs2_assert_warn(sdp, fp))
  463. return -EIO;
  464. kfree(fp);
  465. return 0;
  466. }
  467. /**
  468. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  469. * @file: the file that points to the dentry (we ignore this)
  470. * @dentry: the dentry that points to the inode to sync
  471. *
  472. * The VFS will flush "normal" data for us. We only need to worry
  473. * about metadata here. For journaled data, we just do a log flush
  474. * as we can't avoid it. Otherwise we can just bale out if datasync
  475. * is set. For stuffed inodes we must flush the log in order to
  476. * ensure that all data is on disk.
  477. *
  478. * The call to write_inode_now() is there to write back metadata and
  479. * the inode itself. It does also try and write the data, but thats
  480. * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
  481. * for us.
  482. *
  483. * Returns: errno
  484. */
  485. static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
  486. {
  487. struct inode *inode = dentry->d_inode;
  488. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  489. int ret = 0;
  490. if (gfs2_is_jdata(GFS2_I(inode))) {
  491. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  492. return 0;
  493. }
  494. if (sync_state != 0) {
  495. if (!datasync)
  496. ret = write_inode_now(inode, 0);
  497. if (gfs2_is_stuffed(GFS2_I(inode)))
  498. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  499. }
  500. return ret;
  501. }
  502. /**
  503. * gfs2_setlease - acquire/release a file lease
  504. * @file: the file pointer
  505. * @arg: lease type
  506. * @fl: file lock
  507. *
  508. * Returns: errno
  509. */
  510. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  511. {
  512. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  513. /*
  514. * We don't currently have a way to enforce a lease across the whole
  515. * cluster; until we do, disable leases (by just returning -EINVAL),
  516. * unless the administrator has requested purely local locking.
  517. */
  518. if (!sdp->sd_args.ar_localflocks)
  519. return -EINVAL;
  520. return generic_setlease(file, arg, fl);
  521. }
  522. /**
  523. * gfs2_lock - acquire/release a posix lock on a file
  524. * @file: the file pointer
  525. * @cmd: either modify or retrieve lock state, possibly wait
  526. * @fl: type and range of lock
  527. *
  528. * Returns: errno
  529. */
  530. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  531. {
  532. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  533. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  534. struct lm_lockname name =
  535. { .ln_number = ip->i_no_addr,
  536. .ln_type = LM_TYPE_PLOCK };
  537. if (!(fl->fl_flags & FL_POSIX))
  538. return -ENOLCK;
  539. if (__mandatory_lock(&ip->i_inode))
  540. return -ENOLCK;
  541. if (cmd == F_CANCELLK) {
  542. /* Hack: */
  543. cmd = F_SETLK;
  544. fl->fl_type = F_UNLCK;
  545. }
  546. if (IS_GETLK(cmd))
  547. return gfs2_lm_plock_get(sdp, &name, file, fl);
  548. else if (fl->fl_type == F_UNLCK)
  549. return gfs2_lm_punlock(sdp, &name, file, fl);
  550. else
  551. return gfs2_lm_plock(sdp, &name, file, cmd, fl);
  552. }
  553. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  554. {
  555. struct gfs2_file *fp = file->private_data;
  556. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  557. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  558. struct gfs2_glock *gl;
  559. unsigned int state;
  560. int flags;
  561. int error = 0;
  562. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  563. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE
  564. | GL_FLOCK;
  565. mutex_lock(&fp->f_fl_mutex);
  566. gl = fl_gh->gh_gl;
  567. if (gl) {
  568. if (fl_gh->gh_state == state)
  569. goto out;
  570. flock_lock_file_wait(file,
  571. &(struct file_lock){.fl_type = F_UNLCK});
  572. gfs2_glock_dq_wait(fl_gh);
  573. gfs2_holder_reinit(state, flags, fl_gh);
  574. } else {
  575. error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
  576. ip->i_no_addr, &gfs2_flock_glops,
  577. CREATE, &gl);
  578. if (error)
  579. goto out;
  580. gfs2_holder_init(gl, state, flags, fl_gh);
  581. gfs2_glock_put(gl);
  582. }
  583. error = gfs2_glock_nq(fl_gh);
  584. if (error) {
  585. gfs2_holder_uninit(fl_gh);
  586. if (error == GLR_TRYFAILED)
  587. error = -EAGAIN;
  588. } else {
  589. error = flock_lock_file_wait(file, fl);
  590. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  591. }
  592. out:
  593. mutex_unlock(&fp->f_fl_mutex);
  594. return error;
  595. }
  596. static void do_unflock(struct file *file, struct file_lock *fl)
  597. {
  598. struct gfs2_file *fp = file->private_data;
  599. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  600. mutex_lock(&fp->f_fl_mutex);
  601. flock_lock_file_wait(file, fl);
  602. if (fl_gh->gh_gl)
  603. gfs2_glock_dq_uninit(fl_gh);
  604. mutex_unlock(&fp->f_fl_mutex);
  605. }
  606. /**
  607. * gfs2_flock - acquire/release a flock lock on a file
  608. * @file: the file pointer
  609. * @cmd: either modify or retrieve lock state, possibly wait
  610. * @fl: type and range of lock
  611. *
  612. * Returns: errno
  613. */
  614. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  615. {
  616. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  617. if (!(fl->fl_flags & FL_FLOCK))
  618. return -ENOLCK;
  619. if (__mandatory_lock(&ip->i_inode))
  620. return -ENOLCK;
  621. if (fl->fl_type == F_UNLCK) {
  622. do_unflock(file, fl);
  623. return 0;
  624. } else {
  625. return do_flock(file, cmd, fl);
  626. }
  627. }
  628. const struct file_operations gfs2_file_fops = {
  629. .llseek = gfs2_llseek,
  630. .read = do_sync_read,
  631. .aio_read = generic_file_aio_read,
  632. .write = do_sync_write,
  633. .aio_write = generic_file_aio_write,
  634. .unlocked_ioctl = gfs2_ioctl,
  635. .mmap = gfs2_mmap,
  636. .open = gfs2_open,
  637. .release = gfs2_close,
  638. .fsync = gfs2_fsync,
  639. .lock = gfs2_lock,
  640. .flock = gfs2_flock,
  641. .splice_read = generic_file_splice_read,
  642. .splice_write = generic_file_splice_write,
  643. .setlease = gfs2_setlease,
  644. };
  645. const struct file_operations gfs2_dir_fops = {
  646. .readdir = gfs2_readdir,
  647. .unlocked_ioctl = gfs2_ioctl,
  648. .open = gfs2_open,
  649. .release = gfs2_close,
  650. .fsync = gfs2_fsync,
  651. .lock = gfs2_lock,
  652. .flock = gfs2_flock,
  653. };
  654. const struct file_operations gfs2_file_fops_nolock = {
  655. .llseek = gfs2_llseek,
  656. .read = do_sync_read,
  657. .aio_read = generic_file_aio_read,
  658. .write = do_sync_write,
  659. .aio_write = generic_file_aio_write,
  660. .unlocked_ioctl = gfs2_ioctl,
  661. .mmap = gfs2_mmap,
  662. .open = gfs2_open,
  663. .release = gfs2_close,
  664. .fsync = gfs2_fsync,
  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_nolock = {
  670. .readdir = gfs2_readdir,
  671. .unlocked_ioctl = gfs2_ioctl,
  672. .open = gfs2_open,
  673. .release = gfs2_close,
  674. .fsync = gfs2_fsync,
  675. };