file.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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_for_stable_page(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. .remap_pages = generic_file_remap_pages,
  422. };
  423. /**
  424. * gfs2_mmap -
  425. * @file: The file to map
  426. * @vma: The VMA which described the mapping
  427. *
  428. * There is no need to get a lock here unless we should be updating
  429. * atime. We ignore any locking errors since the only consequence is
  430. * a missed atime update (which will just be deferred until later).
  431. *
  432. * Returns: 0
  433. */
  434. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  435. {
  436. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  437. if (!(file->f_flags & O_NOATIME) &&
  438. !IS_NOATIME(&ip->i_inode)) {
  439. struct gfs2_holder i_gh;
  440. int error;
  441. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  442. error = gfs2_glock_nq(&i_gh);
  443. if (error == 0) {
  444. file_accessed(file);
  445. gfs2_glock_dq(&i_gh);
  446. }
  447. gfs2_holder_uninit(&i_gh);
  448. if (error)
  449. return error;
  450. }
  451. vma->vm_ops = &gfs2_vm_ops;
  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. };