file.c 26 KB

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