file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 (!is_owner_or_cap(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. struct gfs2_holder i_gh;
  403. int error;
  404. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  405. error = gfs2_glock_nq(&i_gh);
  406. file_accessed(file);
  407. if (error == 0)
  408. gfs2_glock_dq_uninit(&i_gh);
  409. }
  410. vma->vm_ops = &gfs2_vm_ops;
  411. vma->vm_flags |= VM_CAN_NONLINEAR;
  412. return 0;
  413. }
  414. /**
  415. * gfs2_open - open a file
  416. * @inode: the inode to open
  417. * @file: the struct file for this opening
  418. *
  419. * Returns: errno
  420. */
  421. static int gfs2_open(struct inode *inode, struct file *file)
  422. {
  423. struct gfs2_inode *ip = GFS2_I(inode);
  424. struct gfs2_holder i_gh;
  425. struct gfs2_file *fp;
  426. int error;
  427. fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
  428. if (!fp)
  429. return -ENOMEM;
  430. mutex_init(&fp->f_fl_mutex);
  431. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  432. file->private_data = fp;
  433. if (S_ISREG(ip->i_inode.i_mode)) {
  434. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  435. &i_gh);
  436. if (error)
  437. goto fail;
  438. if (!(file->f_flags & O_LARGEFILE) &&
  439. i_size_read(inode) > MAX_NON_LFS) {
  440. error = -EOVERFLOW;
  441. goto fail_gunlock;
  442. }
  443. gfs2_glock_dq_uninit(&i_gh);
  444. }
  445. return 0;
  446. fail_gunlock:
  447. gfs2_glock_dq_uninit(&i_gh);
  448. fail:
  449. file->private_data = NULL;
  450. kfree(fp);
  451. return error;
  452. }
  453. /**
  454. * gfs2_close - called to close a struct file
  455. * @inode: the inode the struct file belongs to
  456. * @file: the struct file being closed
  457. *
  458. * Returns: errno
  459. */
  460. static int gfs2_close(struct inode *inode, struct file *file)
  461. {
  462. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  463. struct gfs2_file *fp;
  464. fp = file->private_data;
  465. file->private_data = NULL;
  466. if (gfs2_assert_warn(sdp, fp))
  467. return -EIO;
  468. kfree(fp);
  469. return 0;
  470. }
  471. /**
  472. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  473. * @file: the file that points to the dentry (we ignore this)
  474. * @dentry: the dentry that points to the inode to sync
  475. *
  476. * The VFS will flush "normal" data for us. We only need to worry
  477. * about metadata here. For journaled data, we just do a log flush
  478. * as we can't avoid it. Otherwise we can just bale out if datasync
  479. * is set. For stuffed inodes we must flush the log in order to
  480. * ensure that all data is on disk.
  481. *
  482. * The call to write_inode_now() is there to write back metadata and
  483. * the inode itself. It does also try and write the data, but thats
  484. * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
  485. * for us.
  486. *
  487. * Returns: errno
  488. */
  489. static int gfs2_fsync(struct file *file, int datasync)
  490. {
  491. struct inode *inode = file->f_mapping->host;
  492. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  493. int ret = 0;
  494. if (gfs2_is_jdata(GFS2_I(inode))) {
  495. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  496. return 0;
  497. }
  498. if (sync_state != 0) {
  499. if (!datasync)
  500. ret = write_inode_now(inode, 0);
  501. if (gfs2_is_stuffed(GFS2_I(inode)))
  502. gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
  503. }
  504. return ret;
  505. }
  506. /**
  507. * gfs2_file_aio_write - Perform a write to a file
  508. * @iocb: The io context
  509. * @iov: The data to write
  510. * @nr_segs: Number of @iov segments
  511. * @pos: The file position
  512. *
  513. * We have to do a lock/unlock here to refresh the inode size for
  514. * O_APPEND writes, otherwise we can land up writing at the wrong
  515. * offset. There is still a race, but provided the app is using its
  516. * own file locking, this will make O_APPEND work as expected.
  517. *
  518. */
  519. static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  520. unsigned long nr_segs, loff_t pos)
  521. {
  522. struct file *file = iocb->ki_filp;
  523. if (file->f_flags & O_APPEND) {
  524. struct dentry *dentry = file->f_dentry;
  525. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  526. struct gfs2_holder gh;
  527. int ret;
  528. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  529. if (ret)
  530. return ret;
  531. gfs2_glock_dq_uninit(&gh);
  532. }
  533. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  534. }
  535. static void empty_write_end(struct page *page, unsigned from,
  536. unsigned to)
  537. {
  538. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  539. page_zero_new_buffers(page, from, to);
  540. flush_dcache_page(page);
  541. mark_page_accessed(page);
  542. if (!gfs2_is_writeback(ip))
  543. gfs2_page_add_databufs(ip, page, from, to);
  544. block_commit_write(page, from, to);
  545. }
  546. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  547. {
  548. unsigned start, end, next;
  549. struct buffer_head *bh, *head;
  550. int error;
  551. if (!page_has_buffers(page)) {
  552. error = __block_write_begin(page, from, to - from, gfs2_block_map);
  553. if (unlikely(error))
  554. return error;
  555. empty_write_end(page, from, to);
  556. return 0;
  557. }
  558. bh = head = page_buffers(page);
  559. next = end = 0;
  560. while (next < from) {
  561. next += bh->b_size;
  562. bh = bh->b_this_page;
  563. }
  564. start = next;
  565. do {
  566. next += bh->b_size;
  567. if (buffer_mapped(bh)) {
  568. if (end) {
  569. error = __block_write_begin(page, start, end - start,
  570. gfs2_block_map);
  571. if (unlikely(error))
  572. return error;
  573. empty_write_end(page, start, end);
  574. end = 0;
  575. }
  576. start = next;
  577. }
  578. else
  579. end = next;
  580. bh = bh->b_this_page;
  581. } while (next < to);
  582. if (end) {
  583. error = __block_write_begin(page, start, end - start, gfs2_block_map);
  584. if (unlikely(error))
  585. return error;
  586. empty_write_end(page, start, end);
  587. }
  588. return 0;
  589. }
  590. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  591. int mode)
  592. {
  593. struct gfs2_inode *ip = GFS2_I(inode);
  594. struct buffer_head *dibh;
  595. int error;
  596. u64 start = offset >> PAGE_CACHE_SHIFT;
  597. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  598. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  599. pgoff_t curr;
  600. struct page *page;
  601. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  602. unsigned int from, to;
  603. if (!end_offset)
  604. end_offset = PAGE_CACHE_SIZE;
  605. error = gfs2_meta_inode_buffer(ip, &dibh);
  606. if (unlikely(error))
  607. goto out;
  608. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  609. if (gfs2_is_stuffed(ip)) {
  610. error = gfs2_unstuff_dinode(ip, NULL);
  611. if (unlikely(error))
  612. goto out;
  613. }
  614. curr = start;
  615. offset = start << PAGE_CACHE_SHIFT;
  616. from = start_offset;
  617. to = PAGE_CACHE_SIZE;
  618. while (curr <= end) {
  619. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  620. AOP_FLAG_NOFS);
  621. if (unlikely(!page)) {
  622. error = -ENOMEM;
  623. goto out;
  624. }
  625. if (curr == end)
  626. to = end_offset;
  627. error = write_empty_blocks(page, from, to);
  628. if (!error && offset + to > inode->i_size &&
  629. !(mode & FALLOC_FL_KEEP_SIZE)) {
  630. i_size_write(inode, offset + to);
  631. }
  632. unlock_page(page);
  633. page_cache_release(page);
  634. if (error)
  635. goto out;
  636. curr++;
  637. offset += PAGE_CACHE_SIZE;
  638. from = 0;
  639. }
  640. gfs2_dinode_out(ip, dibh->b_data);
  641. mark_inode_dirty(inode);
  642. brelse(dibh);
  643. out:
  644. return error;
  645. }
  646. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  647. unsigned int *data_blocks, unsigned int *ind_blocks)
  648. {
  649. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  650. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  651. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  652. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  653. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  654. max_data -= tmp;
  655. }
  656. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  657. so it might end up with fewer data blocks */
  658. if (max_data <= *data_blocks)
  659. return;
  660. *data_blocks = max_data;
  661. *ind_blocks = max_blocks - max_data;
  662. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  663. if (*len > max) {
  664. *len = max;
  665. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  666. }
  667. }
  668. static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
  669. loff_t len)
  670. {
  671. struct inode *inode = file->f_path.dentry->d_inode;
  672. struct gfs2_sbd *sdp = GFS2_SB(inode);
  673. struct gfs2_inode *ip = GFS2_I(inode);
  674. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  675. loff_t bytes, max_bytes;
  676. struct gfs2_alloc *al;
  677. int error;
  678. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  679. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  680. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  681. if (mode & ~FALLOC_FL_KEEP_SIZE)
  682. return -EOPNOTSUPP;
  683. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  684. sdp->sd_sb.sb_bsize_shift;
  685. len = next - offset;
  686. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  687. if (!bytes)
  688. bytes = UINT_MAX;
  689. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  690. error = gfs2_glock_nq(&ip->i_gh);
  691. if (unlikely(error))
  692. goto out_uninit;
  693. if (!gfs2_write_alloc_required(ip, offset, len))
  694. goto out_unlock;
  695. while (len > 0) {
  696. if (len < bytes)
  697. bytes = len;
  698. al = gfs2_alloc_get(ip);
  699. if (!al) {
  700. error = -ENOMEM;
  701. goto out_unlock;
  702. }
  703. error = gfs2_quota_lock_check(ip);
  704. if (error)
  705. goto out_alloc_put;
  706. retry:
  707. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  708. al->al_requested = data_blocks + ind_blocks;
  709. error = gfs2_inplace_reserve(ip);
  710. if (error) {
  711. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  712. bytes >>= 1;
  713. goto retry;
  714. }
  715. goto out_qunlock;
  716. }
  717. max_bytes = bytes;
  718. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  719. al->al_requested = data_blocks + ind_blocks;
  720. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  721. RES_RG_HDR + gfs2_rg_blocks(al);
  722. if (gfs2_is_jdata(ip))
  723. rblocks += data_blocks ? data_blocks : 1;
  724. error = gfs2_trans_begin(sdp, rblocks,
  725. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  726. if (error)
  727. goto out_trans_fail;
  728. error = fallocate_chunk(inode, offset, max_bytes, mode);
  729. gfs2_trans_end(sdp);
  730. if (error)
  731. goto out_trans_fail;
  732. len -= max_bytes;
  733. offset += max_bytes;
  734. gfs2_inplace_release(ip);
  735. gfs2_quota_unlock(ip);
  736. gfs2_alloc_put(ip);
  737. }
  738. goto out_unlock;
  739. out_trans_fail:
  740. gfs2_inplace_release(ip);
  741. out_qunlock:
  742. gfs2_quota_unlock(ip);
  743. out_alloc_put:
  744. gfs2_alloc_put(ip);
  745. out_unlock:
  746. gfs2_glock_dq(&ip->i_gh);
  747. out_uninit:
  748. gfs2_holder_uninit(&ip->i_gh);
  749. return error;
  750. }
  751. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  752. /**
  753. * gfs2_setlease - acquire/release a file lease
  754. * @file: the file pointer
  755. * @arg: lease type
  756. * @fl: file lock
  757. *
  758. * We don't currently have a way to enforce a lease across the whole
  759. * cluster; until we do, disable leases (by just returning -EINVAL),
  760. * unless the administrator has requested purely local locking.
  761. *
  762. * Locking: called under lock_flocks
  763. *
  764. * Returns: errno
  765. */
  766. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  767. {
  768. return -EINVAL;
  769. }
  770. /**
  771. * gfs2_lock - acquire/release a posix lock on a file
  772. * @file: the file pointer
  773. * @cmd: either modify or retrieve lock state, possibly wait
  774. * @fl: type and range of lock
  775. *
  776. * Returns: errno
  777. */
  778. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  779. {
  780. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  781. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  782. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  783. if (!(fl->fl_flags & FL_POSIX))
  784. return -ENOLCK;
  785. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  786. return -ENOLCK;
  787. if (cmd == F_CANCELLK) {
  788. /* Hack: */
  789. cmd = F_SETLK;
  790. fl->fl_type = F_UNLCK;
  791. }
  792. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  793. return -EIO;
  794. if (IS_GETLK(cmd))
  795. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  796. else if (fl->fl_type == F_UNLCK)
  797. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  798. else
  799. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  800. }
  801. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  802. {
  803. struct gfs2_file *fp = file->private_data;
  804. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  805. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  806. struct gfs2_glock *gl;
  807. unsigned int state;
  808. int flags;
  809. int error = 0;
  810. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  811. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  812. mutex_lock(&fp->f_fl_mutex);
  813. gl = fl_gh->gh_gl;
  814. if (gl) {
  815. if (fl_gh->gh_state == state)
  816. goto out;
  817. flock_lock_file_wait(file,
  818. &(struct file_lock){.fl_type = F_UNLCK});
  819. gfs2_glock_dq_wait(fl_gh);
  820. gfs2_holder_reinit(state, flags, fl_gh);
  821. } else {
  822. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  823. &gfs2_flock_glops, CREATE, &gl);
  824. if (error)
  825. goto out;
  826. gfs2_holder_init(gl, state, flags, fl_gh);
  827. gfs2_glock_put(gl);
  828. }
  829. error = gfs2_glock_nq(fl_gh);
  830. if (error) {
  831. gfs2_holder_uninit(fl_gh);
  832. if (error == GLR_TRYFAILED)
  833. error = -EAGAIN;
  834. } else {
  835. error = flock_lock_file_wait(file, fl);
  836. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  837. }
  838. out:
  839. mutex_unlock(&fp->f_fl_mutex);
  840. return error;
  841. }
  842. static void do_unflock(struct file *file, struct file_lock *fl)
  843. {
  844. struct gfs2_file *fp = file->private_data;
  845. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  846. mutex_lock(&fp->f_fl_mutex);
  847. flock_lock_file_wait(file, fl);
  848. if (fl_gh->gh_gl)
  849. gfs2_glock_dq_uninit(fl_gh);
  850. mutex_unlock(&fp->f_fl_mutex);
  851. }
  852. /**
  853. * gfs2_flock - acquire/release a flock lock on a file
  854. * @file: the file pointer
  855. * @cmd: either modify or retrieve lock state, possibly wait
  856. * @fl: type and range of lock
  857. *
  858. * Returns: errno
  859. */
  860. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  861. {
  862. if (!(fl->fl_flags & FL_FLOCK))
  863. return -ENOLCK;
  864. if (fl->fl_type & LOCK_MAND)
  865. return -EOPNOTSUPP;
  866. if (fl->fl_type == F_UNLCK) {
  867. do_unflock(file, fl);
  868. return 0;
  869. } else {
  870. return do_flock(file, cmd, fl);
  871. }
  872. }
  873. const struct file_operations gfs2_file_fops = {
  874. .llseek = gfs2_llseek,
  875. .read = do_sync_read,
  876. .aio_read = generic_file_aio_read,
  877. .write = do_sync_write,
  878. .aio_write = gfs2_file_aio_write,
  879. .unlocked_ioctl = gfs2_ioctl,
  880. .mmap = gfs2_mmap,
  881. .open = gfs2_open,
  882. .release = gfs2_close,
  883. .fsync = gfs2_fsync,
  884. .lock = gfs2_lock,
  885. .flock = gfs2_flock,
  886. .splice_read = generic_file_splice_read,
  887. .splice_write = generic_file_splice_write,
  888. .setlease = gfs2_setlease,
  889. .fallocate = gfs2_fallocate,
  890. };
  891. const struct file_operations gfs2_dir_fops = {
  892. .readdir = gfs2_readdir,
  893. .unlocked_ioctl = gfs2_ioctl,
  894. .open = gfs2_open,
  895. .release = gfs2_close,
  896. .fsync = gfs2_fsync,
  897. .lock = gfs2_lock,
  898. .flock = gfs2_flock,
  899. .llseek = default_llseek,
  900. };
  901. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  902. const struct file_operations gfs2_file_fops_nolock = {
  903. .llseek = gfs2_llseek,
  904. .read = do_sync_read,
  905. .aio_read = generic_file_aio_read,
  906. .write = do_sync_write,
  907. .aio_write = gfs2_file_aio_write,
  908. .unlocked_ioctl = gfs2_ioctl,
  909. .mmap = gfs2_mmap,
  910. .open = gfs2_open,
  911. .release = gfs2_close,
  912. .fsync = gfs2_fsync,
  913. .splice_read = generic_file_splice_read,
  914. .splice_write = generic_file_splice_write,
  915. .setlease = generic_setlease,
  916. .fallocate = gfs2_fallocate,
  917. };
  918. const struct file_operations gfs2_dir_fops_nolock = {
  919. .readdir = gfs2_readdir,
  920. .unlocked_ioctl = gfs2_ioctl,
  921. .open = gfs2_open,
  922. .release = gfs2_close,
  923. .fsync = gfs2_fsync,
  924. .llseek = default_llseek,
  925. };