file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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/falloc.h>
  22. #include <linux/swap.h>
  23. #include <linux/crc32.h>
  24. #include <linux/writeback.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/dlm.h>
  27. #include <linux/dlm_plock.h>
  28. #include "gfs2.h"
  29. #include "incore.h"
  30. #include "bmap.h"
  31. #include "dir.h"
  32. #include "glock.h"
  33. #include "glops.h"
  34. #include "inode.h"
  35. #include "log.h"
  36. #include "meta_io.h"
  37. #include "quota.h"
  38. #include "rgrp.h"
  39. #include "trans.h"
  40. #include "util.h"
  41. /**
  42. * gfs2_llseek - seek to a location in a file
  43. * @file: the file
  44. * @offset: the offset
  45. * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  46. *
  47. * SEEK_END requires the glock for the file because it references the
  48. * file's size.
  49. *
  50. * Returns: The new offset, or errno
  51. */
  52. static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
  53. {
  54. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  55. struct gfs2_holder i_gh;
  56. loff_t error;
  57. switch (origin) {
  58. case SEEK_END: /* These reference inode->i_size */
  59. case SEEK_DATA:
  60. case SEEK_HOLE:
  61. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  62. &i_gh);
  63. if (!error) {
  64. error = generic_file_llseek(file, offset, origin);
  65. gfs2_glock_dq_uninit(&i_gh);
  66. }
  67. break;
  68. case SEEK_CUR:
  69. case SEEK_SET:
  70. error = generic_file_llseek(file, offset, origin);
  71. break;
  72. default:
  73. error = -EINVAL;
  74. }
  75. return error;
  76. }
  77. /**
  78. * gfs2_readdir - Read directory entries from a directory
  79. * @file: The directory to read from
  80. * @dirent: Buffer for dirents
  81. * @filldir: Function used to do the copying
  82. *
  83. * Returns: errno
  84. */
  85. static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
  86. {
  87. struct inode *dir = file->f_mapping->host;
  88. struct gfs2_inode *dip = GFS2_I(dir);
  89. struct gfs2_holder d_gh;
  90. u64 offset = file->f_pos;
  91. int error;
  92. gfs2_holder_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  93. error = gfs2_glock_nq(&d_gh);
  94. if (error) {
  95. gfs2_holder_uninit(&d_gh);
  96. return error;
  97. }
  98. error = gfs2_dir_read(dir, &offset, dirent, filldir);
  99. gfs2_glock_dq_uninit(&d_gh);
  100. file->f_pos = offset;
  101. return error;
  102. }
  103. /**
  104. * fsflags_cvt
  105. * @table: A table of 32 u32 flags
  106. * @val: a 32 bit value to convert
  107. *
  108. * This function can be used to convert between fsflags values and
  109. * GFS2's own flags values.
  110. *
  111. * Returns: the converted flags
  112. */
  113. static u32 fsflags_cvt(const u32 *table, u32 val)
  114. {
  115. u32 res = 0;
  116. while(val) {
  117. if (val & 1)
  118. res |= *table;
  119. table++;
  120. val >>= 1;
  121. }
  122. return res;
  123. }
  124. static const u32 fsflags_to_gfs2[32] = {
  125. [3] = GFS2_DIF_SYNC,
  126. [4] = GFS2_DIF_IMMUTABLE,
  127. [5] = GFS2_DIF_APPENDONLY,
  128. [7] = GFS2_DIF_NOATIME,
  129. [12] = GFS2_DIF_EXHASH,
  130. [14] = GFS2_DIF_INHERIT_JDATA,
  131. };
  132. static const u32 gfs2_to_fsflags[32] = {
  133. [gfs2fl_Sync] = FS_SYNC_FL,
  134. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  135. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  136. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  137. [gfs2fl_ExHash] = FS_INDEX_FL,
  138. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  139. };
  140. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  141. {
  142. struct inode *inode = filp->f_path.dentry->d_inode;
  143. struct gfs2_inode *ip = GFS2_I(inode);
  144. struct gfs2_holder gh;
  145. int error;
  146. u32 fsflags;
  147. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  148. error = gfs2_glock_nq(&gh);
  149. if (error)
  150. return error;
  151. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_diskflags);
  152. if (!S_ISDIR(inode->i_mode) && ip->i_diskflags & GFS2_DIF_JDATA)
  153. fsflags |= FS_JOURNAL_DATA_FL;
  154. if (put_user(fsflags, ptr))
  155. error = -EFAULT;
  156. gfs2_glock_dq(&gh);
  157. gfs2_holder_uninit(&gh);
  158. return error;
  159. }
  160. void gfs2_set_inode_flags(struct inode *inode)
  161. {
  162. struct gfs2_inode *ip = GFS2_I(inode);
  163. unsigned int flags = inode->i_flags;
  164. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  165. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  166. inode->i_flags |= S_NOSEC;
  167. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  168. flags |= S_IMMUTABLE;
  169. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  170. flags |= S_APPEND;
  171. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  172. flags |= S_NOATIME;
  173. if (ip->i_diskflags & GFS2_DIF_SYNC)
  174. flags |= S_SYNC;
  175. inode->i_flags = flags;
  176. }
  177. /* Flags that can be set by user space */
  178. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  179. GFS2_DIF_IMMUTABLE| \
  180. GFS2_DIF_APPENDONLY| \
  181. GFS2_DIF_NOATIME| \
  182. GFS2_DIF_SYNC| \
  183. GFS2_DIF_SYSTEM| \
  184. GFS2_DIF_INHERIT_JDATA)
  185. /**
  186. * gfs2_set_flags - set flags on an inode
  187. * @inode: The inode
  188. * @flags: The flags to set
  189. * @mask: Indicates which flags are valid
  190. *
  191. */
  192. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  193. {
  194. struct inode *inode = filp->f_path.dentry->d_inode;
  195. struct gfs2_inode *ip = GFS2_I(inode);
  196. struct gfs2_sbd *sdp = GFS2_SB(inode);
  197. struct buffer_head *bh;
  198. struct gfs2_holder gh;
  199. int error;
  200. u32 new_flags, flags;
  201. error = mnt_want_write(filp->f_path.mnt);
  202. if (error)
  203. return error;
  204. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  205. if (error)
  206. goto out_drop_write;
  207. error = -EACCES;
  208. if (!inode_owner_or_capable(inode))
  209. goto out;
  210. error = 0;
  211. flags = ip->i_diskflags;
  212. new_flags = (flags & ~mask) | (reqflags & mask);
  213. if ((new_flags ^ flags) == 0)
  214. goto out;
  215. error = -EINVAL;
  216. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  217. goto out;
  218. error = -EPERM;
  219. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  220. goto out;
  221. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  222. goto out;
  223. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  224. !capable(CAP_LINUX_IMMUTABLE))
  225. goto out;
  226. if (!IS_IMMUTABLE(inode)) {
  227. error = gfs2_permission(inode, MAY_WRITE);
  228. if (error)
  229. goto out;
  230. }
  231. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  232. if (flags & GFS2_DIF_JDATA)
  233. gfs2_log_flush(sdp, ip->i_gl);
  234. error = filemap_fdatawrite(inode->i_mapping);
  235. if (error)
  236. goto out;
  237. error = filemap_fdatawait(inode->i_mapping);
  238. if (error)
  239. goto out;
  240. }
  241. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  242. if (error)
  243. goto out;
  244. error = gfs2_meta_inode_buffer(ip, &bh);
  245. if (error)
  246. goto out_trans_end;
  247. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  248. ip->i_diskflags = new_flags;
  249. gfs2_dinode_out(ip, bh->b_data);
  250. brelse(bh);
  251. gfs2_set_inode_flags(inode);
  252. gfs2_set_aops(inode);
  253. out_trans_end:
  254. gfs2_trans_end(sdp);
  255. out:
  256. gfs2_glock_dq_uninit(&gh);
  257. out_drop_write:
  258. mnt_drop_write(filp->f_path.mnt);
  259. return error;
  260. }
  261. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  262. {
  263. struct inode *inode = filp->f_path.dentry->d_inode;
  264. u32 fsflags, gfsflags;
  265. if (get_user(fsflags, ptr))
  266. return -EFAULT;
  267. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  268. if (!S_ISDIR(inode->i_mode)) {
  269. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  270. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  271. return do_gfs2_set_flags(filp, gfsflags, ~0);
  272. }
  273. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  274. }
  275. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  276. {
  277. switch(cmd) {
  278. case FS_IOC_GETFLAGS:
  279. return gfs2_get_flags(filp, (u32 __user *)arg);
  280. case FS_IOC_SETFLAGS:
  281. return gfs2_set_flags(filp, (u32 __user *)arg);
  282. }
  283. return -ENOTTY;
  284. }
  285. /**
  286. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  287. * @page: The (locked) page to allocate backing for
  288. *
  289. * We try to allocate all the blocks required for the page in
  290. * one go. This might fail for various reasons, so we keep
  291. * trying until all the blocks to back this page are allocated.
  292. * If some of the blocks are already allocated, thats ok too.
  293. */
  294. static int gfs2_allocate_page_backing(struct page *page)
  295. {
  296. struct inode *inode = page->mapping->host;
  297. struct buffer_head bh;
  298. unsigned long size = PAGE_CACHE_SIZE;
  299. u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  300. do {
  301. bh.b_state = 0;
  302. bh.b_size = size;
  303. gfs2_block_map(inode, lblock, &bh, 1);
  304. if (!buffer_mapped(&bh))
  305. return -EIO;
  306. size -= bh.b_size;
  307. lblock += (bh.b_size >> inode->i_blkbits);
  308. } while(size > 0);
  309. return 0;
  310. }
  311. /**
  312. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  313. * @vma: The virtual memory area
  314. * @page: The page which is about to become writable
  315. *
  316. * When the page becomes writable, we need to ensure that we have
  317. * blocks allocated on disk to back that page.
  318. */
  319. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  320. {
  321. struct page *page = vmf->page;
  322. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  323. struct gfs2_inode *ip = GFS2_I(inode);
  324. struct gfs2_sbd *sdp = GFS2_SB(inode);
  325. unsigned long last_index;
  326. u64 pos = page->index << PAGE_CACHE_SHIFT;
  327. unsigned int data_blocks, ind_blocks, rblocks;
  328. struct gfs2_holder gh;
  329. struct gfs2_alloc *al;
  330. loff_t size;
  331. int ret;
  332. /* Wait if fs is frozen. This is racy so we check again later on
  333. * and retry if the fs has been frozen after the page lock has
  334. * been acquired
  335. */
  336. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  337. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  338. ret = gfs2_glock_nq(&gh);
  339. if (ret)
  340. goto out;
  341. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  342. set_bit(GIF_SW_PAGED, &ip->i_flags);
  343. if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
  344. lock_page(page);
  345. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  346. ret = -EAGAIN;
  347. unlock_page(page);
  348. }
  349. goto out_unlock;
  350. }
  351. ret = -ENOMEM;
  352. al = gfs2_alloc_get(ip);
  353. if (al == NULL)
  354. goto out_unlock;
  355. ret = gfs2_quota_lock_check(ip);
  356. if (ret)
  357. goto out_alloc_put;
  358. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  359. al->al_requested = data_blocks + ind_blocks;
  360. ret = gfs2_inplace_reserve(ip);
  361. if (ret)
  362. goto out_quota_unlock;
  363. rblocks = RES_DINODE + ind_blocks;
  364. if (gfs2_is_jdata(ip))
  365. rblocks += data_blocks ? data_blocks : 1;
  366. if (ind_blocks || data_blocks) {
  367. rblocks += RES_STATFS + RES_QUOTA;
  368. rblocks += gfs2_rg_blocks(ip);
  369. }
  370. ret = gfs2_trans_begin(sdp, rblocks, 0);
  371. if (ret)
  372. goto out_trans_fail;
  373. lock_page(page);
  374. ret = -EINVAL;
  375. size = i_size_read(inode);
  376. last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  377. /* Check page index against inode size */
  378. if (size == 0 || (page->index > last_index))
  379. goto out_trans_end;
  380. ret = -EAGAIN;
  381. /* If truncated, we must retry the operation, we may have raced
  382. * with the glock demotion code.
  383. */
  384. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  385. goto out_trans_end;
  386. /* Unstuff, if required, and allocate backing blocks for page */
  387. ret = 0;
  388. if (gfs2_is_stuffed(ip))
  389. ret = gfs2_unstuff_dinode(ip, page);
  390. if (ret == 0)
  391. ret = gfs2_allocate_page_backing(page);
  392. out_trans_end:
  393. if (ret)
  394. unlock_page(page);
  395. gfs2_trans_end(sdp);
  396. out_trans_fail:
  397. gfs2_inplace_release(ip);
  398. out_quota_unlock:
  399. gfs2_quota_unlock(ip);
  400. out_alloc_put:
  401. gfs2_alloc_put(ip);
  402. out_unlock:
  403. gfs2_glock_dq(&gh);
  404. out:
  405. gfs2_holder_uninit(&gh);
  406. if (ret == 0) {
  407. set_page_dirty(page);
  408. /* This check must be post dropping of transaction lock */
  409. if (inode->i_sb->s_frozen == SB_UNFROZEN) {
  410. wait_on_page_writeback(page);
  411. } else {
  412. ret = -EAGAIN;
  413. unlock_page(page);
  414. }
  415. }
  416. return block_page_mkwrite_return(ret);
  417. }
  418. static const struct vm_operations_struct gfs2_vm_ops = {
  419. .fault = filemap_fault,
  420. .page_mkwrite = gfs2_page_mkwrite,
  421. };
  422. /**
  423. * gfs2_mmap -
  424. * @file: The file to map
  425. * @vma: The VMA which described the mapping
  426. *
  427. * There is no need to get a lock here unless we should be updating
  428. * atime. We ignore any locking errors since the only consequence is
  429. * a missed atime update (which will just be deferred until later).
  430. *
  431. * Returns: 0
  432. */
  433. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  434. {
  435. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  436. if (!(file->f_flags & O_NOATIME) &&
  437. !IS_NOATIME(&ip->i_inode)) {
  438. struct gfs2_holder i_gh;
  439. int error;
  440. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  441. error = gfs2_glock_nq(&i_gh);
  442. if (error == 0) {
  443. file_accessed(file);
  444. gfs2_glock_dq(&i_gh);
  445. }
  446. gfs2_holder_uninit(&i_gh);
  447. if (error)
  448. return error;
  449. }
  450. vma->vm_ops = &gfs2_vm_ops;
  451. vma->vm_flags |= VM_CAN_NONLINEAR;
  452. return 0;
  453. }
  454. /**
  455. * gfs2_open - open a file
  456. * @inode: the inode to open
  457. * @file: the struct file for this opening
  458. *
  459. * Returns: errno
  460. */
  461. static int gfs2_open(struct inode *inode, struct file *file)
  462. {
  463. struct gfs2_inode *ip = GFS2_I(inode);
  464. struct gfs2_holder i_gh;
  465. struct gfs2_file *fp;
  466. int error;
  467. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  468. if (!fp)
  469. return -ENOMEM;
  470. mutex_init(&fp->f_fl_mutex);
  471. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  472. file->private_data = fp;
  473. if (S_ISREG(ip->i_inode.i_mode)) {
  474. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  475. &i_gh);
  476. if (error)
  477. goto fail;
  478. if (!(file->f_flags & O_LARGEFILE) &&
  479. i_size_read(inode) > MAX_NON_LFS) {
  480. error = -EOVERFLOW;
  481. goto fail_gunlock;
  482. }
  483. gfs2_glock_dq_uninit(&i_gh);
  484. }
  485. return 0;
  486. fail_gunlock:
  487. gfs2_glock_dq_uninit(&i_gh);
  488. fail:
  489. file->private_data = NULL;
  490. kfree(fp);
  491. return error;
  492. }
  493. /**
  494. * gfs2_close - called to close a struct file
  495. * @inode: the inode the struct file belongs to
  496. * @file: the struct file being closed
  497. *
  498. * Returns: errno
  499. */
  500. static int gfs2_close(struct inode *inode, struct file *file)
  501. {
  502. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  503. struct gfs2_file *fp;
  504. fp = file->private_data;
  505. file->private_data = NULL;
  506. if (gfs2_assert_warn(sdp, fp))
  507. return -EIO;
  508. kfree(fp);
  509. return 0;
  510. }
  511. /**
  512. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  513. * @file: the file that points to the dentry
  514. * @start: the start position in the file to sync
  515. * @end: the end position in the file to sync
  516. * @datasync: set if we can ignore timestamp changes
  517. *
  518. * We split the data flushing here so that we don't wait for the data
  519. * until after we've also sent the metadata to disk. Note that for
  520. * data=ordered, we will write & wait for the data at the log flush
  521. * stage anyway, so this is unlikely to make much of a difference
  522. * except in the data=writeback case.
  523. *
  524. * If the fdatawrite fails due to any reason except -EIO, we will
  525. * continue the remainder of the fsync, although we'll still report
  526. * the error at the end. This is to match filemap_write_and_wait_range()
  527. * behaviour.
  528. *
  529. * Returns: errno
  530. */
  531. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  532. int datasync)
  533. {
  534. struct address_space *mapping = file->f_mapping;
  535. struct inode *inode = mapping->host;
  536. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  537. struct gfs2_inode *ip = GFS2_I(inode);
  538. int ret, ret1 = 0;
  539. if (mapping->nrpages) {
  540. ret1 = filemap_fdatawrite_range(mapping, start, end);
  541. if (ret1 == -EIO)
  542. return ret1;
  543. }
  544. if (datasync)
  545. sync_state &= ~I_DIRTY_SYNC;
  546. if (sync_state) {
  547. ret = sync_inode_metadata(inode, 1);
  548. if (ret)
  549. return ret;
  550. if (gfs2_is_jdata(ip))
  551. filemap_write_and_wait(mapping);
  552. gfs2_ail_flush(ip->i_gl, 1);
  553. }
  554. if (mapping->nrpages)
  555. ret = filemap_fdatawait_range(mapping, start, end);
  556. return ret ? ret : ret1;
  557. }
  558. /**
  559. * gfs2_file_aio_write - Perform a write to a file
  560. * @iocb: The io context
  561. * @iov: The data to write
  562. * @nr_segs: Number of @iov segments
  563. * @pos: The file position
  564. *
  565. * We have to do a lock/unlock here to refresh the inode size for
  566. * O_APPEND writes, otherwise we can land up writing at the wrong
  567. * offset. There is still a race, but provided the app is using its
  568. * own file locking, this will make O_APPEND work as expected.
  569. *
  570. */
  571. static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  572. unsigned long nr_segs, loff_t pos)
  573. {
  574. struct file *file = iocb->ki_filp;
  575. if (file->f_flags & O_APPEND) {
  576. struct dentry *dentry = file->f_dentry;
  577. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  578. struct gfs2_holder gh;
  579. int ret;
  580. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  581. if (ret)
  582. return ret;
  583. gfs2_glock_dq_uninit(&gh);
  584. }
  585. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  586. }
  587. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  588. int mode)
  589. {
  590. struct gfs2_inode *ip = GFS2_I(inode);
  591. struct buffer_head *dibh;
  592. int error;
  593. unsigned int nr_blks;
  594. sector_t lblock = offset >> inode->i_blkbits;
  595. error = gfs2_meta_inode_buffer(ip, &dibh);
  596. if (unlikely(error))
  597. return error;
  598. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  599. if (gfs2_is_stuffed(ip)) {
  600. error = gfs2_unstuff_dinode(ip, NULL);
  601. if (unlikely(error))
  602. goto out;
  603. }
  604. while (len) {
  605. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  606. bh_map.b_size = len;
  607. set_buffer_zeronew(&bh_map);
  608. error = gfs2_block_map(inode, lblock, &bh_map, 1);
  609. if (unlikely(error))
  610. goto out;
  611. len -= bh_map.b_size;
  612. nr_blks = bh_map.b_size >> inode->i_blkbits;
  613. lblock += nr_blks;
  614. if (!buffer_new(&bh_map))
  615. continue;
  616. if (unlikely(!buffer_zeronew(&bh_map))) {
  617. error = -EIO;
  618. goto out;
  619. }
  620. }
  621. if (offset + len > inode->i_size && !(mode & FALLOC_FL_KEEP_SIZE))
  622. i_size_write(inode, offset + len);
  623. mark_inode_dirty(inode);
  624. out:
  625. brelse(dibh);
  626. return error;
  627. }
  628. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  629. unsigned int *data_blocks, unsigned int *ind_blocks)
  630. {
  631. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  632. unsigned int max_blocks = ip->i_rgd->rd_free_clone;
  633. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  634. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  635. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  636. max_data -= tmp;
  637. }
  638. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  639. so it might end up with fewer data blocks */
  640. if (max_data <= *data_blocks)
  641. return;
  642. *data_blocks = max_data;
  643. *ind_blocks = max_blocks - max_data;
  644. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  645. if (*len > max) {
  646. *len = max;
  647. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  648. }
  649. }
  650. static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
  651. loff_t len)
  652. {
  653. struct inode *inode = file->f_path.dentry->d_inode;
  654. struct gfs2_sbd *sdp = GFS2_SB(inode);
  655. struct gfs2_inode *ip = GFS2_I(inode);
  656. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  657. loff_t bytes, max_bytes;
  658. struct gfs2_alloc *al;
  659. int error;
  660. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  661. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  662. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  663. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  664. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  665. if (mode & ~FALLOC_FL_KEEP_SIZE)
  666. return -EOPNOTSUPP;
  667. offset &= bsize_mask;
  668. len = next - offset;
  669. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  670. if (!bytes)
  671. bytes = UINT_MAX;
  672. bytes &= bsize_mask;
  673. if (bytes == 0)
  674. bytes = sdp->sd_sb.sb_bsize;
  675. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  676. error = gfs2_glock_nq(&ip->i_gh);
  677. if (unlikely(error))
  678. goto out_uninit;
  679. if (!gfs2_write_alloc_required(ip, offset, len))
  680. goto out_unlock;
  681. while (len > 0) {
  682. if (len < bytes)
  683. bytes = len;
  684. al = gfs2_alloc_get(ip);
  685. if (!al) {
  686. error = -ENOMEM;
  687. goto out_unlock;
  688. }
  689. error = gfs2_quota_lock_check(ip);
  690. if (error)
  691. goto out_alloc_put;
  692. retry:
  693. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  694. al->al_requested = data_blocks + ind_blocks;
  695. error = gfs2_inplace_reserve(ip);
  696. if (error) {
  697. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  698. bytes >>= 1;
  699. bytes &= bsize_mask;
  700. if (bytes == 0)
  701. bytes = sdp->sd_sb.sb_bsize;
  702. goto retry;
  703. }
  704. goto out_qunlock;
  705. }
  706. max_bytes = bytes;
  707. calc_max_reserv(ip, (len > max_chunk_size)? max_chunk_size: len,
  708. &max_bytes, &data_blocks, &ind_blocks);
  709. al->al_requested = data_blocks + ind_blocks;
  710. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  711. RES_RG_HDR + gfs2_rg_blocks(ip);
  712. if (gfs2_is_jdata(ip))
  713. rblocks += data_blocks ? data_blocks : 1;
  714. error = gfs2_trans_begin(sdp, rblocks,
  715. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  716. if (error)
  717. goto out_trans_fail;
  718. error = fallocate_chunk(inode, offset, max_bytes, mode);
  719. gfs2_trans_end(sdp);
  720. if (error)
  721. goto out_trans_fail;
  722. len -= max_bytes;
  723. offset += max_bytes;
  724. gfs2_inplace_release(ip);
  725. gfs2_quota_unlock(ip);
  726. gfs2_alloc_put(ip);
  727. }
  728. goto out_unlock;
  729. out_trans_fail:
  730. gfs2_inplace_release(ip);
  731. out_qunlock:
  732. gfs2_quota_unlock(ip);
  733. out_alloc_put:
  734. gfs2_alloc_put(ip);
  735. out_unlock:
  736. gfs2_glock_dq(&ip->i_gh);
  737. out_uninit:
  738. gfs2_holder_uninit(&ip->i_gh);
  739. return error;
  740. }
  741. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  742. /**
  743. * gfs2_setlease - acquire/release a file lease
  744. * @file: the file pointer
  745. * @arg: lease type
  746. * @fl: file lock
  747. *
  748. * We don't currently have a way to enforce a lease across the whole
  749. * cluster; until we do, disable leases (by just returning -EINVAL),
  750. * unless the administrator has requested purely local locking.
  751. *
  752. * Locking: called under lock_flocks
  753. *
  754. * Returns: errno
  755. */
  756. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  757. {
  758. return -EINVAL;
  759. }
  760. /**
  761. * gfs2_lock - acquire/release a posix lock on a file
  762. * @file: the file pointer
  763. * @cmd: either modify or retrieve lock state, possibly wait
  764. * @fl: type and range of lock
  765. *
  766. * Returns: errno
  767. */
  768. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  769. {
  770. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  771. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  772. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  773. if (!(fl->fl_flags & FL_POSIX))
  774. return -ENOLCK;
  775. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  776. return -ENOLCK;
  777. if (cmd == F_CANCELLK) {
  778. /* Hack: */
  779. cmd = F_SETLK;
  780. fl->fl_type = F_UNLCK;
  781. }
  782. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  783. return -EIO;
  784. if (IS_GETLK(cmd))
  785. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  786. else if (fl->fl_type == F_UNLCK)
  787. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  788. else
  789. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  790. }
  791. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  792. {
  793. struct gfs2_file *fp = file->private_data;
  794. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  795. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  796. struct gfs2_glock *gl;
  797. unsigned int state;
  798. int flags;
  799. int error = 0;
  800. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  801. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  802. mutex_lock(&fp->f_fl_mutex);
  803. gl = fl_gh->gh_gl;
  804. if (gl) {
  805. if (fl_gh->gh_state == state)
  806. goto out;
  807. flock_lock_file_wait(file,
  808. &(struct file_lock){.fl_type = F_UNLCK});
  809. gfs2_glock_dq_wait(fl_gh);
  810. gfs2_holder_reinit(state, flags, fl_gh);
  811. } else {
  812. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  813. &gfs2_flock_glops, CREATE, &gl);
  814. if (error)
  815. goto out;
  816. gfs2_holder_init(gl, state, flags, fl_gh);
  817. gfs2_glock_put(gl);
  818. }
  819. error = gfs2_glock_nq(fl_gh);
  820. if (error) {
  821. gfs2_holder_uninit(fl_gh);
  822. if (error == GLR_TRYFAILED)
  823. error = -EAGAIN;
  824. } else {
  825. error = flock_lock_file_wait(file, fl);
  826. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  827. }
  828. out:
  829. mutex_unlock(&fp->f_fl_mutex);
  830. return error;
  831. }
  832. static void do_unflock(struct file *file, struct file_lock *fl)
  833. {
  834. struct gfs2_file *fp = file->private_data;
  835. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  836. mutex_lock(&fp->f_fl_mutex);
  837. flock_lock_file_wait(file, fl);
  838. if (fl_gh->gh_gl) {
  839. gfs2_glock_dq_wait(fl_gh);
  840. gfs2_holder_uninit(fl_gh);
  841. }
  842. mutex_unlock(&fp->f_fl_mutex);
  843. }
  844. /**
  845. * gfs2_flock - acquire/release a flock lock on a file
  846. * @file: the file pointer
  847. * @cmd: either modify or retrieve lock state, possibly wait
  848. * @fl: type and range of lock
  849. *
  850. * Returns: errno
  851. */
  852. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  853. {
  854. if (!(fl->fl_flags & FL_FLOCK))
  855. return -ENOLCK;
  856. if (fl->fl_type & LOCK_MAND)
  857. return -EOPNOTSUPP;
  858. if (fl->fl_type == F_UNLCK) {
  859. do_unflock(file, fl);
  860. return 0;
  861. } else {
  862. return do_flock(file, cmd, fl);
  863. }
  864. }
  865. const struct file_operations gfs2_file_fops = {
  866. .llseek = gfs2_llseek,
  867. .read = do_sync_read,
  868. .aio_read = generic_file_aio_read,
  869. .write = do_sync_write,
  870. .aio_write = gfs2_file_aio_write,
  871. .unlocked_ioctl = gfs2_ioctl,
  872. .mmap = gfs2_mmap,
  873. .open = gfs2_open,
  874. .release = gfs2_close,
  875. .fsync = gfs2_fsync,
  876. .lock = gfs2_lock,
  877. .flock = gfs2_flock,
  878. .splice_read = generic_file_splice_read,
  879. .splice_write = generic_file_splice_write,
  880. .setlease = gfs2_setlease,
  881. .fallocate = gfs2_fallocate,
  882. };
  883. const struct file_operations gfs2_dir_fops = {
  884. .readdir = gfs2_readdir,
  885. .unlocked_ioctl = gfs2_ioctl,
  886. .open = gfs2_open,
  887. .release = gfs2_close,
  888. .fsync = gfs2_fsync,
  889. .lock = gfs2_lock,
  890. .flock = gfs2_flock,
  891. .llseek = default_llseek,
  892. };
  893. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  894. const struct file_operations gfs2_file_fops_nolock = {
  895. .llseek = gfs2_llseek,
  896. .read = do_sync_read,
  897. .aio_read = generic_file_aio_read,
  898. .write = do_sync_write,
  899. .aio_write = gfs2_file_aio_write,
  900. .unlocked_ioctl = gfs2_ioctl,
  901. .mmap = gfs2_mmap,
  902. .open = gfs2_open,
  903. .release = gfs2_close,
  904. .fsync = gfs2_fsync,
  905. .splice_read = generic_file_splice_read,
  906. .splice_write = generic_file_splice_write,
  907. .setlease = generic_setlease,
  908. .fallocate = gfs2_fallocate,
  909. };
  910. const struct file_operations gfs2_dir_fops_nolock = {
  911. .readdir = gfs2_readdir,
  912. .unlocked_ioctl = gfs2_ioctl,
  913. .open = gfs2_open,
  914. .release = gfs2_close,
  915. .fsync = gfs2_fsync,
  916. .llseek = default_llseek,
  917. };