file.c 26 KB

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