file.c 26 KB

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