file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. sb_start_pagefault(inode->i_sb);
  337. /* Update file times before taking page lock */
  338. file_update_time(vma->vm_file);
  339. ret = gfs2_rs_alloc(ip);
  340. if (ret)
  341. return ret;
  342. atomic_set(&ip->i_res->rs_sizehint,
  343. PAGE_CACHE_SIZE >> sdp->sd_sb.sb_bsize_shift);
  344. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  345. ret = gfs2_glock_nq(&gh);
  346. if (ret)
  347. goto out;
  348. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  349. set_bit(GIF_SW_PAGED, &ip->i_flags);
  350. if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
  351. lock_page(page);
  352. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  353. ret = -EAGAIN;
  354. unlock_page(page);
  355. }
  356. goto out_unlock;
  357. }
  358. ret = gfs2_rindex_update(sdp);
  359. if (ret)
  360. goto out_unlock;
  361. ret = gfs2_quota_lock_check(ip);
  362. if (ret)
  363. goto out_unlock;
  364. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  365. ret = gfs2_inplace_reserve(ip, data_blocks + ind_blocks);
  366. if (ret)
  367. goto out_quota_unlock;
  368. rblocks = RES_DINODE + ind_blocks;
  369. if (gfs2_is_jdata(ip))
  370. rblocks += data_blocks ? data_blocks : 1;
  371. if (ind_blocks || data_blocks) {
  372. rblocks += RES_STATFS + RES_QUOTA;
  373. rblocks += gfs2_rg_blocks(ip);
  374. }
  375. ret = gfs2_trans_begin(sdp, rblocks, 0);
  376. if (ret)
  377. goto out_trans_fail;
  378. lock_page(page);
  379. ret = -EINVAL;
  380. size = i_size_read(inode);
  381. last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  382. /* Check page index against inode size */
  383. if (size == 0 || (page->index > last_index))
  384. goto out_trans_end;
  385. ret = -EAGAIN;
  386. /* If truncated, we must retry the operation, we may have raced
  387. * with the glock demotion code.
  388. */
  389. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  390. goto out_trans_end;
  391. /* Unstuff, if required, and allocate backing blocks for page */
  392. ret = 0;
  393. if (gfs2_is_stuffed(ip))
  394. ret = gfs2_unstuff_dinode(ip, page);
  395. if (ret == 0)
  396. ret = gfs2_allocate_page_backing(page);
  397. out_trans_end:
  398. if (ret)
  399. unlock_page(page);
  400. gfs2_trans_end(sdp);
  401. out_trans_fail:
  402. gfs2_inplace_release(ip);
  403. out_quota_unlock:
  404. gfs2_quota_unlock(ip);
  405. out_unlock:
  406. gfs2_glock_dq(&gh);
  407. out:
  408. gfs2_holder_uninit(&gh);
  409. if (ret == 0) {
  410. set_page_dirty(page);
  411. wait_on_page_writeback(page);
  412. }
  413. sb_end_pagefault(inode->i_sb);
  414. return block_page_mkwrite_return(ret);
  415. }
  416. static const struct vm_operations_struct gfs2_vm_ops = {
  417. .fault = filemap_fault,
  418. .page_mkwrite = gfs2_page_mkwrite,
  419. };
  420. /**
  421. * gfs2_mmap -
  422. * @file: The file to map
  423. * @vma: The VMA which described the mapping
  424. *
  425. * There is no need to get a lock here unless we should be updating
  426. * atime. We ignore any locking errors since the only consequence is
  427. * a missed atime update (which will just be deferred until later).
  428. *
  429. * Returns: 0
  430. */
  431. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  432. {
  433. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  434. if (!(file->f_flags & O_NOATIME) &&
  435. !IS_NOATIME(&ip->i_inode)) {
  436. struct gfs2_holder i_gh;
  437. int error;
  438. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  439. error = gfs2_glock_nq(&i_gh);
  440. if (error == 0) {
  441. file_accessed(file);
  442. gfs2_glock_dq(&i_gh);
  443. }
  444. gfs2_holder_uninit(&i_gh);
  445. if (error)
  446. return error;
  447. }
  448. vma->vm_ops = &gfs2_vm_ops;
  449. vma->vm_flags |= VM_CAN_NONLINEAR;
  450. return 0;
  451. }
  452. /**
  453. * gfs2_open - open a file
  454. * @inode: the inode to open
  455. * @file: the struct file for this opening
  456. *
  457. * Returns: errno
  458. */
  459. static int gfs2_open(struct inode *inode, struct file *file)
  460. {
  461. struct gfs2_inode *ip = GFS2_I(inode);
  462. struct gfs2_holder i_gh;
  463. struct gfs2_file *fp;
  464. int error;
  465. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  466. if (!fp)
  467. return -ENOMEM;
  468. mutex_init(&fp->f_fl_mutex);
  469. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  470. file->private_data = fp;
  471. if (S_ISREG(ip->i_inode.i_mode)) {
  472. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  473. &i_gh);
  474. if (error)
  475. goto fail;
  476. if (!(file->f_flags & O_LARGEFILE) &&
  477. i_size_read(inode) > MAX_NON_LFS) {
  478. error = -EOVERFLOW;
  479. goto fail_gunlock;
  480. }
  481. gfs2_glock_dq_uninit(&i_gh);
  482. }
  483. return 0;
  484. fail_gunlock:
  485. gfs2_glock_dq_uninit(&i_gh);
  486. fail:
  487. file->private_data = NULL;
  488. kfree(fp);
  489. return error;
  490. }
  491. /**
  492. * gfs2_release - called to close a struct file
  493. * @inode: the inode the struct file belongs to
  494. * @file: the struct file being closed
  495. *
  496. * Returns: errno
  497. */
  498. static int gfs2_release(struct inode *inode, struct file *file)
  499. {
  500. struct gfs2_inode *ip = GFS2_I(inode);
  501. kfree(file->private_data);
  502. file->private_data = NULL;
  503. if ((file->f_mode & FMODE_WRITE) &&
  504. (atomic_read(&inode->i_writecount) == 1))
  505. gfs2_rs_delete(ip);
  506. return 0;
  507. }
  508. /**
  509. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  510. * @file: the file that points to the dentry
  511. * @start: the start position in the file to sync
  512. * @end: the end position in the file to sync
  513. * @datasync: set if we can ignore timestamp changes
  514. *
  515. * We split the data flushing here so that we don't wait for the data
  516. * until after we've also sent the metadata to disk. Note that for
  517. * data=ordered, we will write & wait for the data at the log flush
  518. * stage anyway, so this is unlikely to make much of a difference
  519. * except in the data=writeback case.
  520. *
  521. * If the fdatawrite fails due to any reason except -EIO, we will
  522. * continue the remainder of the fsync, although we'll still report
  523. * the error at the end. This is to match filemap_write_and_wait_range()
  524. * behaviour.
  525. *
  526. * Returns: errno
  527. */
  528. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  529. int datasync)
  530. {
  531. struct address_space *mapping = file->f_mapping;
  532. struct inode *inode = mapping->host;
  533. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  534. struct gfs2_inode *ip = GFS2_I(inode);
  535. int ret = 0, ret1 = 0;
  536. if (mapping->nrpages) {
  537. ret1 = filemap_fdatawrite_range(mapping, start, end);
  538. if (ret1 == -EIO)
  539. return ret1;
  540. }
  541. if (datasync)
  542. sync_state &= ~I_DIRTY_SYNC;
  543. if (sync_state) {
  544. ret = sync_inode_metadata(inode, 1);
  545. if (ret)
  546. return ret;
  547. if (gfs2_is_jdata(ip))
  548. filemap_write_and_wait(mapping);
  549. gfs2_ail_flush(ip->i_gl, 1);
  550. }
  551. if (mapping->nrpages)
  552. ret = filemap_fdatawait_range(mapping, start, end);
  553. return ret ? ret : ret1;
  554. }
  555. /**
  556. * gfs2_file_aio_write - Perform a write to a file
  557. * @iocb: The io context
  558. * @iov: The data to write
  559. * @nr_segs: Number of @iov segments
  560. * @pos: The file position
  561. *
  562. * We have to do a lock/unlock here to refresh the inode size for
  563. * O_APPEND writes, otherwise we can land up writing at the wrong
  564. * offset. There is still a race, but provided the app is using its
  565. * own file locking, this will make O_APPEND work as expected.
  566. *
  567. */
  568. static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  569. unsigned long nr_segs, loff_t pos)
  570. {
  571. struct file *file = iocb->ki_filp;
  572. size_t writesize = iov_length(iov, nr_segs);
  573. struct dentry *dentry = file->f_dentry;
  574. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  575. struct gfs2_sbd *sdp;
  576. int ret;
  577. sdp = GFS2_SB(file->f_mapping->host);
  578. ret = gfs2_rs_alloc(ip);
  579. if (ret)
  580. return ret;
  581. atomic_set(&ip->i_res->rs_sizehint, writesize >> sdp->sd_sb.sb_bsize_shift);
  582. if (file->f_flags & O_APPEND) {
  583. struct gfs2_holder gh;
  584. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  585. if (ret)
  586. return ret;
  587. gfs2_glock_dq_uninit(&gh);
  588. }
  589. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  590. }
  591. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  592. int mode)
  593. {
  594. struct gfs2_inode *ip = GFS2_I(inode);
  595. struct buffer_head *dibh;
  596. int error;
  597. loff_t size = len;
  598. unsigned int nr_blks;
  599. sector_t lblock = offset >> inode->i_blkbits;
  600. error = gfs2_meta_inode_buffer(ip, &dibh);
  601. if (unlikely(error))
  602. return error;
  603. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  604. if (gfs2_is_stuffed(ip)) {
  605. error = gfs2_unstuff_dinode(ip, NULL);
  606. if (unlikely(error))
  607. goto out;
  608. }
  609. while (len) {
  610. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  611. bh_map.b_size = len;
  612. set_buffer_zeronew(&bh_map);
  613. error = gfs2_block_map(inode, lblock, &bh_map, 1);
  614. if (unlikely(error))
  615. goto out;
  616. len -= bh_map.b_size;
  617. nr_blks = bh_map.b_size >> inode->i_blkbits;
  618. lblock += nr_blks;
  619. if (!buffer_new(&bh_map))
  620. continue;
  621. if (unlikely(!buffer_zeronew(&bh_map))) {
  622. error = -EIO;
  623. goto out;
  624. }
  625. }
  626. if (offset + size > inode->i_size && !(mode & FALLOC_FL_KEEP_SIZE))
  627. i_size_write(inode, offset + size);
  628. mark_inode_dirty(inode);
  629. out:
  630. brelse(dibh);
  631. return error;
  632. }
  633. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  634. unsigned int *data_blocks, unsigned int *ind_blocks)
  635. {
  636. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  637. unsigned int max_blocks = ip->i_rgd->rd_free_clone;
  638. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  639. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  640. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  641. max_data -= tmp;
  642. }
  643. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  644. so it might end up with fewer data blocks */
  645. if (max_data <= *data_blocks)
  646. return;
  647. *data_blocks = max_data;
  648. *ind_blocks = max_blocks - max_data;
  649. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  650. if (*len > max) {
  651. *len = max;
  652. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  653. }
  654. }
  655. static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
  656. loff_t len)
  657. {
  658. struct inode *inode = file->f_path.dentry->d_inode;
  659. struct gfs2_sbd *sdp = GFS2_SB(inode);
  660. struct gfs2_inode *ip = GFS2_I(inode);
  661. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  662. loff_t bytes, max_bytes;
  663. int error;
  664. const loff_t pos = offset;
  665. const loff_t count = len;
  666. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  667. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  668. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  669. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  670. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  671. if (mode & ~FALLOC_FL_KEEP_SIZE)
  672. return -EOPNOTSUPP;
  673. offset &= bsize_mask;
  674. len = next - offset;
  675. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  676. if (!bytes)
  677. bytes = UINT_MAX;
  678. bytes &= bsize_mask;
  679. if (bytes == 0)
  680. bytes = sdp->sd_sb.sb_bsize;
  681. error = gfs2_rs_alloc(ip);
  682. if (error)
  683. return error;
  684. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  685. error = gfs2_glock_nq(&ip->i_gh);
  686. if (unlikely(error))
  687. goto out_uninit;
  688. atomic_set(&ip->i_res->rs_sizehint, len >> sdp->sd_sb.sb_bsize_shift);
  689. while (len > 0) {
  690. if (len < bytes)
  691. bytes = len;
  692. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  693. len -= bytes;
  694. offset += bytes;
  695. continue;
  696. }
  697. error = gfs2_quota_lock_check(ip);
  698. if (error)
  699. goto out_unlock;
  700. retry:
  701. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  702. error = gfs2_inplace_reserve(ip, data_blocks + ind_blocks);
  703. if (error) {
  704. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  705. bytes >>= 1;
  706. bytes &= bsize_mask;
  707. if (bytes == 0)
  708. bytes = sdp->sd_sb.sb_bsize;
  709. goto retry;
  710. }
  711. goto out_qunlock;
  712. }
  713. max_bytes = bytes;
  714. calc_max_reserv(ip, (len > max_chunk_size)? max_chunk_size: len,
  715. &max_bytes, &data_blocks, &ind_blocks);
  716. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  717. RES_RG_HDR + gfs2_rg_blocks(ip);
  718. if (gfs2_is_jdata(ip))
  719. rblocks += data_blocks ? data_blocks : 1;
  720. error = gfs2_trans_begin(sdp, rblocks,
  721. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  722. if (error)
  723. goto out_trans_fail;
  724. error = fallocate_chunk(inode, offset, max_bytes, mode);
  725. gfs2_trans_end(sdp);
  726. if (error)
  727. goto out_trans_fail;
  728. len -= max_bytes;
  729. offset += max_bytes;
  730. gfs2_inplace_release(ip);
  731. gfs2_quota_unlock(ip);
  732. }
  733. if (error == 0)
  734. error = generic_write_sync(file, pos, count);
  735. goto out_unlock;
  736. out_trans_fail:
  737. gfs2_inplace_release(ip);
  738. out_qunlock:
  739. gfs2_quota_unlock(ip);
  740. out_unlock:
  741. gfs2_glock_dq(&ip->i_gh);
  742. out_uninit:
  743. gfs2_holder_uninit(&ip->i_gh);
  744. return error;
  745. }
  746. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  747. /**
  748. * gfs2_setlease - acquire/release a file lease
  749. * @file: the file pointer
  750. * @arg: lease type
  751. * @fl: file lock
  752. *
  753. * We don't currently have a way to enforce a lease across the whole
  754. * cluster; until we do, disable leases (by just returning -EINVAL),
  755. * unless the administrator has requested purely local locking.
  756. *
  757. * Locking: called under lock_flocks
  758. *
  759. * Returns: errno
  760. */
  761. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  762. {
  763. return -EINVAL;
  764. }
  765. /**
  766. * gfs2_lock - acquire/release a posix lock on a file
  767. * @file: the file pointer
  768. * @cmd: either modify or retrieve lock state, possibly wait
  769. * @fl: type and range of lock
  770. *
  771. * Returns: errno
  772. */
  773. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  774. {
  775. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  776. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  777. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  778. if (!(fl->fl_flags & FL_POSIX))
  779. return -ENOLCK;
  780. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  781. return -ENOLCK;
  782. if (cmd == F_CANCELLK) {
  783. /* Hack: */
  784. cmd = F_SETLK;
  785. fl->fl_type = F_UNLCK;
  786. }
  787. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  788. return -EIO;
  789. if (IS_GETLK(cmd))
  790. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  791. else if (fl->fl_type == F_UNLCK)
  792. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  793. else
  794. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  795. }
  796. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  797. {
  798. struct gfs2_file *fp = file->private_data;
  799. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  800. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  801. struct gfs2_glock *gl;
  802. unsigned int state;
  803. int flags;
  804. int error = 0;
  805. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  806. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  807. mutex_lock(&fp->f_fl_mutex);
  808. gl = fl_gh->gh_gl;
  809. if (gl) {
  810. if (fl_gh->gh_state == state)
  811. goto out;
  812. flock_lock_file_wait(file,
  813. &(struct file_lock){.fl_type = F_UNLCK});
  814. gfs2_glock_dq_wait(fl_gh);
  815. gfs2_holder_reinit(state, flags, fl_gh);
  816. } else {
  817. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  818. &gfs2_flock_glops, CREATE, &gl);
  819. if (error)
  820. goto out;
  821. gfs2_holder_init(gl, state, flags, fl_gh);
  822. gfs2_glock_put(gl);
  823. }
  824. error = gfs2_glock_nq(fl_gh);
  825. if (error) {
  826. gfs2_holder_uninit(fl_gh);
  827. if (error == GLR_TRYFAILED)
  828. error = -EAGAIN;
  829. } else {
  830. error = flock_lock_file_wait(file, fl);
  831. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  832. }
  833. out:
  834. mutex_unlock(&fp->f_fl_mutex);
  835. return error;
  836. }
  837. static void do_unflock(struct file *file, struct file_lock *fl)
  838. {
  839. struct gfs2_file *fp = file->private_data;
  840. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  841. mutex_lock(&fp->f_fl_mutex);
  842. flock_lock_file_wait(file, fl);
  843. if (fl_gh->gh_gl) {
  844. gfs2_glock_dq_wait(fl_gh);
  845. gfs2_holder_uninit(fl_gh);
  846. }
  847. mutex_unlock(&fp->f_fl_mutex);
  848. }
  849. /**
  850. * gfs2_flock - acquire/release a flock lock on a file
  851. * @file: the file pointer
  852. * @cmd: either modify or retrieve lock state, possibly wait
  853. * @fl: type and range of lock
  854. *
  855. * Returns: errno
  856. */
  857. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  858. {
  859. if (!(fl->fl_flags & FL_FLOCK))
  860. return -ENOLCK;
  861. if (fl->fl_type & LOCK_MAND)
  862. return -EOPNOTSUPP;
  863. if (fl->fl_type == F_UNLCK) {
  864. do_unflock(file, fl);
  865. return 0;
  866. } else {
  867. return do_flock(file, cmd, fl);
  868. }
  869. }
  870. const struct file_operations gfs2_file_fops = {
  871. .llseek = gfs2_llseek,
  872. .read = do_sync_read,
  873. .aio_read = generic_file_aio_read,
  874. .write = do_sync_write,
  875. .aio_write = gfs2_file_aio_write,
  876. .unlocked_ioctl = gfs2_ioctl,
  877. .mmap = gfs2_mmap,
  878. .open = gfs2_open,
  879. .release = gfs2_release,
  880. .fsync = gfs2_fsync,
  881. .lock = gfs2_lock,
  882. .flock = gfs2_flock,
  883. .splice_read = generic_file_splice_read,
  884. .splice_write = generic_file_splice_write,
  885. .setlease = gfs2_setlease,
  886. .fallocate = gfs2_fallocate,
  887. };
  888. const struct file_operations gfs2_dir_fops = {
  889. .readdir = gfs2_readdir,
  890. .unlocked_ioctl = gfs2_ioctl,
  891. .open = gfs2_open,
  892. .release = gfs2_release,
  893. .fsync = gfs2_fsync,
  894. .lock = gfs2_lock,
  895. .flock = gfs2_flock,
  896. .llseek = default_llseek,
  897. };
  898. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  899. const struct file_operations gfs2_file_fops_nolock = {
  900. .llseek = gfs2_llseek,
  901. .read = do_sync_read,
  902. .aio_read = generic_file_aio_read,
  903. .write = do_sync_write,
  904. .aio_write = gfs2_file_aio_write,
  905. .unlocked_ioctl = gfs2_ioctl,
  906. .mmap = gfs2_mmap,
  907. .open = gfs2_open,
  908. .release = gfs2_release,
  909. .fsync = gfs2_fsync,
  910. .splice_read = generic_file_splice_read,
  911. .splice_write = generic_file_splice_write,
  912. .setlease = generic_setlease,
  913. .fallocate = gfs2_fallocate,
  914. };
  915. const struct file_operations gfs2_dir_fops_nolock = {
  916. .readdir = gfs2_readdir,
  917. .unlocked_ioctl = gfs2_ioctl,
  918. .open = gfs2_open,
  919. .release = gfs2_release,
  920. .fsync = gfs2_fsync,
  921. .llseek = default_llseek,
  922. };