file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. };
  131. static const u32 gfs2_to_fsflags[32] = {
  132. [gfs2fl_Sync] = FS_SYNC_FL,
  133. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  134. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  135. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  136. [gfs2fl_ExHash] = FS_INDEX_FL,
  137. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  138. };
  139. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  140. {
  141. struct inode *inode = filp->f_path.dentry->d_inode;
  142. struct gfs2_inode *ip = GFS2_I(inode);
  143. struct gfs2_holder gh;
  144. int error;
  145. u32 fsflags;
  146. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  147. error = gfs2_glock_nq(&gh);
  148. if (error)
  149. return error;
  150. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_diskflags);
  151. if (!S_ISDIR(inode->i_mode) && ip->i_diskflags & GFS2_DIF_JDATA)
  152. fsflags |= FS_JOURNAL_DATA_FL;
  153. if (put_user(fsflags, ptr))
  154. error = -EFAULT;
  155. gfs2_glock_dq(&gh);
  156. gfs2_holder_uninit(&gh);
  157. return error;
  158. }
  159. void gfs2_set_inode_flags(struct inode *inode)
  160. {
  161. struct gfs2_inode *ip = GFS2_I(inode);
  162. unsigned int flags = inode->i_flags;
  163. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  164. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  165. inode->i_flags |= S_NOSEC;
  166. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  167. flags |= S_IMMUTABLE;
  168. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  169. flags |= S_APPEND;
  170. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  171. flags |= S_NOATIME;
  172. if (ip->i_diskflags & GFS2_DIF_SYNC)
  173. flags |= S_SYNC;
  174. inode->i_flags = flags;
  175. }
  176. /* Flags that can be set by user space */
  177. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  178. GFS2_DIF_IMMUTABLE| \
  179. GFS2_DIF_APPENDONLY| \
  180. GFS2_DIF_NOATIME| \
  181. GFS2_DIF_SYNC| \
  182. GFS2_DIF_SYSTEM| \
  183. GFS2_DIF_INHERIT_JDATA)
  184. /**
  185. * gfs2_set_flags - set flags on an inode
  186. * @inode: The inode
  187. * @flags: The flags to set
  188. * @mask: Indicates which flags are valid
  189. *
  190. */
  191. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  192. {
  193. struct inode *inode = filp->f_path.dentry->d_inode;
  194. struct gfs2_inode *ip = GFS2_I(inode);
  195. struct gfs2_sbd *sdp = GFS2_SB(inode);
  196. struct buffer_head *bh;
  197. struct gfs2_holder gh;
  198. int error;
  199. u32 new_flags, flags;
  200. error = mnt_want_write_file(filp);
  201. if (error)
  202. return error;
  203. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  204. if (error)
  205. goto out_drop_write;
  206. error = -EACCES;
  207. if (!inode_owner_or_capable(inode))
  208. goto out;
  209. error = 0;
  210. flags = ip->i_diskflags;
  211. new_flags = (flags & ~mask) | (reqflags & mask);
  212. if ((new_flags ^ flags) == 0)
  213. goto out;
  214. error = -EINVAL;
  215. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  216. goto out;
  217. error = -EPERM;
  218. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  219. goto out;
  220. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  221. goto out;
  222. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  223. !capable(CAP_LINUX_IMMUTABLE))
  224. goto out;
  225. if (!IS_IMMUTABLE(inode)) {
  226. error = gfs2_permission(inode, MAY_WRITE);
  227. if (error)
  228. goto out;
  229. }
  230. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  231. if (flags & GFS2_DIF_JDATA)
  232. gfs2_log_flush(sdp, ip->i_gl);
  233. error = filemap_fdatawrite(inode->i_mapping);
  234. if (error)
  235. goto out;
  236. error = filemap_fdatawait(inode->i_mapping);
  237. if (error)
  238. goto out;
  239. }
  240. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  241. if (error)
  242. goto out;
  243. error = gfs2_meta_inode_buffer(ip, &bh);
  244. if (error)
  245. goto out_trans_end;
  246. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  247. ip->i_diskflags = new_flags;
  248. gfs2_dinode_out(ip, bh->b_data);
  249. brelse(bh);
  250. gfs2_set_inode_flags(inode);
  251. gfs2_set_aops(inode);
  252. out_trans_end:
  253. gfs2_trans_end(sdp);
  254. out:
  255. gfs2_glock_dq_uninit(&gh);
  256. out_drop_write:
  257. mnt_drop_write_file(filp);
  258. return error;
  259. }
  260. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  261. {
  262. struct inode *inode = filp->f_path.dentry->d_inode;
  263. u32 fsflags, gfsflags;
  264. if (get_user(fsflags, ptr))
  265. return -EFAULT;
  266. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  267. if (!S_ISDIR(inode->i_mode)) {
  268. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  269. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  270. return do_gfs2_set_flags(filp, gfsflags, ~0);
  271. }
  272. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  273. }
  274. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  275. {
  276. switch(cmd) {
  277. case FS_IOC_GETFLAGS:
  278. return gfs2_get_flags(filp, (u32 __user *)arg);
  279. case FS_IOC_SETFLAGS:
  280. return gfs2_set_flags(filp, (u32 __user *)arg);
  281. case FITRIM:
  282. return gfs2_fitrim(filp, (void __user *)arg);
  283. }
  284. return -ENOTTY;
  285. }
  286. /**
  287. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  288. * @page: The (locked) page to allocate backing for
  289. *
  290. * We try to allocate all the blocks required for the page in
  291. * one go. This might fail for various reasons, so we keep
  292. * trying until all the blocks to back this page are allocated.
  293. * If some of the blocks are already allocated, thats ok too.
  294. */
  295. static int gfs2_allocate_page_backing(struct page *page)
  296. {
  297. struct inode *inode = page->mapping->host;
  298. struct buffer_head bh;
  299. unsigned long size = PAGE_CACHE_SIZE;
  300. u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  301. do {
  302. bh.b_state = 0;
  303. bh.b_size = size;
  304. gfs2_block_map(inode, lblock, &bh, 1);
  305. if (!buffer_mapped(&bh))
  306. return -EIO;
  307. size -= bh.b_size;
  308. lblock += (bh.b_size >> inode->i_blkbits);
  309. } while(size > 0);
  310. return 0;
  311. }
  312. /**
  313. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  314. * @vma: The virtual memory area
  315. * @page: The page which is about to become writable
  316. *
  317. * When the page becomes writable, we need to ensure that we have
  318. * blocks allocated on disk to back that page.
  319. */
  320. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  321. {
  322. struct page *page = vmf->page;
  323. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  324. struct gfs2_inode *ip = GFS2_I(inode);
  325. struct gfs2_sbd *sdp = GFS2_SB(inode);
  326. unsigned long last_index;
  327. u64 pos = page->index << PAGE_CACHE_SHIFT;
  328. unsigned int data_blocks, ind_blocks, rblocks;
  329. struct gfs2_holder gh;
  330. struct gfs2_qadata *qa;
  331. loff_t size;
  332. int ret;
  333. /* Wait if fs is frozen. This is racy so we check again later on
  334. * and retry if the fs has been frozen after the page lock has
  335. * been acquired
  336. */
  337. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  338. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  339. ret = gfs2_glock_nq(&gh);
  340. if (ret)
  341. goto out;
  342. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  343. set_bit(GIF_SW_PAGED, &ip->i_flags);
  344. if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
  345. lock_page(page);
  346. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  347. ret = -EAGAIN;
  348. unlock_page(page);
  349. }
  350. goto out_unlock;
  351. }
  352. ret = -ENOMEM;
  353. qa = gfs2_qadata_get(ip);
  354. if (qa == NULL)
  355. goto out_unlock;
  356. ret = gfs2_quota_lock_check(ip);
  357. if (ret)
  358. goto out_alloc_put;
  359. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  360. ret = gfs2_inplace_reserve(ip, data_blocks + ind_blocks);
  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_qadata_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 = 0, 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. loff_t size = len;
  594. unsigned int nr_blks;
  595. sector_t lblock = offset >> inode->i_blkbits;
  596. error = gfs2_meta_inode_buffer(ip, &dibh);
  597. if (unlikely(error))
  598. return error;
  599. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  600. if (gfs2_is_stuffed(ip)) {
  601. error = gfs2_unstuff_dinode(ip, NULL);
  602. if (unlikely(error))
  603. goto out;
  604. }
  605. while (len) {
  606. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  607. bh_map.b_size = len;
  608. set_buffer_zeronew(&bh_map);
  609. error = gfs2_block_map(inode, lblock, &bh_map, 1);
  610. if (unlikely(error))
  611. goto out;
  612. len -= bh_map.b_size;
  613. nr_blks = bh_map.b_size >> inode->i_blkbits;
  614. lblock += nr_blks;
  615. if (!buffer_new(&bh_map))
  616. continue;
  617. if (unlikely(!buffer_zeronew(&bh_map))) {
  618. error = -EIO;
  619. goto out;
  620. }
  621. }
  622. if (offset + size > inode->i_size && !(mode & FALLOC_FL_KEEP_SIZE))
  623. i_size_write(inode, offset + size);
  624. mark_inode_dirty(inode);
  625. out:
  626. brelse(dibh);
  627. return error;
  628. }
  629. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  630. unsigned int *data_blocks, unsigned int *ind_blocks)
  631. {
  632. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  633. unsigned int max_blocks = ip->i_rgd->rd_free_clone;
  634. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  635. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  636. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  637. max_data -= tmp;
  638. }
  639. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  640. so it might end up with fewer data blocks */
  641. if (max_data <= *data_blocks)
  642. return;
  643. *data_blocks = max_data;
  644. *ind_blocks = max_blocks - max_data;
  645. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  646. if (*len > max) {
  647. *len = max;
  648. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  649. }
  650. }
  651. static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
  652. loff_t len)
  653. {
  654. struct inode *inode = file->f_path.dentry->d_inode;
  655. struct gfs2_sbd *sdp = GFS2_SB(inode);
  656. struct gfs2_inode *ip = GFS2_I(inode);
  657. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  658. loff_t bytes, max_bytes;
  659. struct gfs2_qadata *qa;
  660. int error;
  661. const loff_t pos = offset;
  662. const loff_t count = len;
  663. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  664. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  665. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  666. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  667. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  668. if (mode & ~FALLOC_FL_KEEP_SIZE)
  669. return -EOPNOTSUPP;
  670. offset &= bsize_mask;
  671. len = next - offset;
  672. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  673. if (!bytes)
  674. bytes = UINT_MAX;
  675. bytes &= bsize_mask;
  676. if (bytes == 0)
  677. bytes = sdp->sd_sb.sb_bsize;
  678. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  679. error = gfs2_glock_nq(&ip->i_gh);
  680. if (unlikely(error))
  681. goto out_uninit;
  682. while (len > 0) {
  683. if (len < bytes)
  684. bytes = len;
  685. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  686. len -= bytes;
  687. offset += bytes;
  688. continue;
  689. }
  690. qa = gfs2_qadata_get(ip);
  691. if (!qa) {
  692. error = -ENOMEM;
  693. goto out_unlock;
  694. }
  695. error = gfs2_quota_lock_check(ip);
  696. if (error)
  697. goto out_alloc_put;
  698. retry:
  699. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  700. error = gfs2_inplace_reserve(ip, data_blocks + ind_blocks);
  701. if (error) {
  702. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  703. bytes >>= 1;
  704. bytes &= bsize_mask;
  705. if (bytes == 0)
  706. bytes = sdp->sd_sb.sb_bsize;
  707. goto retry;
  708. }
  709. goto out_qunlock;
  710. }
  711. max_bytes = bytes;
  712. calc_max_reserv(ip, (len > max_chunk_size)? max_chunk_size: len,
  713. &max_bytes, &data_blocks, &ind_blocks);
  714. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  715. RES_RG_HDR + gfs2_rg_blocks(ip);
  716. if (gfs2_is_jdata(ip))
  717. rblocks += data_blocks ? data_blocks : 1;
  718. error = gfs2_trans_begin(sdp, rblocks,
  719. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  720. if (error)
  721. goto out_trans_fail;
  722. error = fallocate_chunk(inode, offset, max_bytes, mode);
  723. gfs2_trans_end(sdp);
  724. if (error)
  725. goto out_trans_fail;
  726. len -= max_bytes;
  727. offset += max_bytes;
  728. gfs2_inplace_release(ip);
  729. gfs2_quota_unlock(ip);
  730. gfs2_qadata_put(ip);
  731. }
  732. if (error == 0)
  733. error = generic_write_sync(file, pos, count);
  734. goto out_unlock;
  735. out_trans_fail:
  736. gfs2_inplace_release(ip);
  737. out_qunlock:
  738. gfs2_quota_unlock(ip);
  739. out_alloc_put:
  740. gfs2_qadata_put(ip);
  741. out_unlock:
  742. gfs2_glock_dq(&ip->i_gh);
  743. out_uninit:
  744. gfs2_holder_uninit(&ip->i_gh);
  745. return error;
  746. }
  747. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  748. /**
  749. * gfs2_setlease - acquire/release a file lease
  750. * @file: the file pointer
  751. * @arg: lease type
  752. * @fl: file lock
  753. *
  754. * We don't currently have a way to enforce a lease across the whole
  755. * cluster; until we do, disable leases (by just returning -EINVAL),
  756. * unless the administrator has requested purely local locking.
  757. *
  758. * Locking: called under lock_flocks
  759. *
  760. * Returns: errno
  761. */
  762. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  763. {
  764. return -EINVAL;
  765. }
  766. /**
  767. * gfs2_lock - acquire/release a posix lock on a file
  768. * @file: the file pointer
  769. * @cmd: either modify or retrieve lock state, possibly wait
  770. * @fl: type and range of lock
  771. *
  772. * Returns: errno
  773. */
  774. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  775. {
  776. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  777. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  778. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  779. if (!(fl->fl_flags & FL_POSIX))
  780. return -ENOLCK;
  781. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  782. return -ENOLCK;
  783. if (cmd == F_CANCELLK) {
  784. /* Hack: */
  785. cmd = F_SETLK;
  786. fl->fl_type = F_UNLCK;
  787. }
  788. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  789. return -EIO;
  790. if (IS_GETLK(cmd))
  791. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  792. else if (fl->fl_type == F_UNLCK)
  793. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  794. else
  795. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  796. }
  797. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  798. {
  799. struct gfs2_file *fp = file->private_data;
  800. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  801. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  802. struct gfs2_glock *gl;
  803. unsigned int state;
  804. int flags;
  805. int error = 0;
  806. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  807. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  808. mutex_lock(&fp->f_fl_mutex);
  809. gl = fl_gh->gh_gl;
  810. if (gl) {
  811. if (fl_gh->gh_state == state)
  812. goto out;
  813. flock_lock_file_wait(file,
  814. &(struct file_lock){.fl_type = F_UNLCK});
  815. gfs2_glock_dq_wait(fl_gh);
  816. gfs2_holder_reinit(state, flags, fl_gh);
  817. } else {
  818. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  819. &gfs2_flock_glops, CREATE, &gl);
  820. if (error)
  821. goto out;
  822. gfs2_holder_init(gl, state, flags, fl_gh);
  823. gfs2_glock_put(gl);
  824. }
  825. error = gfs2_glock_nq(fl_gh);
  826. if (error) {
  827. gfs2_holder_uninit(fl_gh);
  828. if (error == GLR_TRYFAILED)
  829. error = -EAGAIN;
  830. } else {
  831. error = flock_lock_file_wait(file, fl);
  832. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  833. }
  834. out:
  835. mutex_unlock(&fp->f_fl_mutex);
  836. return error;
  837. }
  838. static void do_unflock(struct file *file, struct file_lock *fl)
  839. {
  840. struct gfs2_file *fp = file->private_data;
  841. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  842. mutex_lock(&fp->f_fl_mutex);
  843. flock_lock_file_wait(file, fl);
  844. if (fl_gh->gh_gl) {
  845. gfs2_glock_dq_wait(fl_gh);
  846. gfs2_holder_uninit(fl_gh);
  847. }
  848. mutex_unlock(&fp->f_fl_mutex);
  849. }
  850. /**
  851. * gfs2_flock - acquire/release a flock lock on a file
  852. * @file: the file pointer
  853. * @cmd: either modify or retrieve lock state, possibly wait
  854. * @fl: type and range of lock
  855. *
  856. * Returns: errno
  857. */
  858. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  859. {
  860. if (!(fl->fl_flags & FL_FLOCK))
  861. return -ENOLCK;
  862. if (fl->fl_type & LOCK_MAND)
  863. return -EOPNOTSUPP;
  864. if (fl->fl_type == F_UNLCK) {
  865. do_unflock(file, fl);
  866. return 0;
  867. } else {
  868. return do_flock(file, cmd, fl);
  869. }
  870. }
  871. const struct file_operations gfs2_file_fops = {
  872. .llseek = gfs2_llseek,
  873. .read = do_sync_read,
  874. .aio_read = generic_file_aio_read,
  875. .write = do_sync_write,
  876. .aio_write = gfs2_file_aio_write,
  877. .unlocked_ioctl = gfs2_ioctl,
  878. .mmap = gfs2_mmap,
  879. .open = gfs2_open,
  880. .release = gfs2_close,
  881. .fsync = gfs2_fsync,
  882. .lock = gfs2_lock,
  883. .flock = gfs2_flock,
  884. .splice_read = generic_file_splice_read,
  885. .splice_write = generic_file_splice_write,
  886. .setlease = gfs2_setlease,
  887. .fallocate = gfs2_fallocate,
  888. };
  889. const struct file_operations gfs2_dir_fops = {
  890. .readdir = gfs2_readdir,
  891. .unlocked_ioctl = gfs2_ioctl,
  892. .open = gfs2_open,
  893. .release = gfs2_close,
  894. .fsync = gfs2_fsync,
  895. .lock = gfs2_lock,
  896. .flock = gfs2_flock,
  897. .llseek = default_llseek,
  898. };
  899. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  900. const struct file_operations gfs2_file_fops_nolock = {
  901. .llseek = gfs2_llseek,
  902. .read = do_sync_read,
  903. .aio_read = generic_file_aio_read,
  904. .write = do_sync_write,
  905. .aio_write = gfs2_file_aio_write,
  906. .unlocked_ioctl = gfs2_ioctl,
  907. .mmap = gfs2_mmap,
  908. .open = gfs2_open,
  909. .release = gfs2_close,
  910. .fsync = gfs2_fsync,
  911. .splice_read = generic_file_splice_read,
  912. .splice_write = generic_file_splice_write,
  913. .setlease = generic_setlease,
  914. .fallocate = gfs2_fallocate,
  915. };
  916. const struct file_operations gfs2_dir_fops_nolock = {
  917. .readdir = gfs2_readdir,
  918. .unlocked_ioctl = gfs2_ioctl,
  919. .open = gfs2_open,
  920. .release = gfs2_close,
  921. .fsync = gfs2_fsync,
  922. .llseek = default_llseek,
  923. };