file.c 26 KB

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