file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. * @datasync: set if we can ignore timestamp changes
  480. *
  481. * The VFS will flush data for us. We only need to worry
  482. * about metadata here.
  483. *
  484. * Returns: errno
  485. */
  486. static int gfs2_fsync(struct file *file, int datasync)
  487. {
  488. struct inode *inode = file->f_mapping->host;
  489. int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
  490. struct gfs2_inode *ip = GFS2_I(inode);
  491. int ret;
  492. if (datasync)
  493. sync_state &= ~I_DIRTY_SYNC;
  494. if (sync_state) {
  495. ret = sync_inode_metadata(inode, 1);
  496. if (ret)
  497. return ret;
  498. gfs2_ail_flush(ip->i_gl);
  499. }
  500. return 0;
  501. }
  502. /**
  503. * gfs2_file_aio_write - Perform a write to a file
  504. * @iocb: The io context
  505. * @iov: The data to write
  506. * @nr_segs: Number of @iov segments
  507. * @pos: The file position
  508. *
  509. * We have to do a lock/unlock here to refresh the inode size for
  510. * O_APPEND writes, otherwise we can land up writing at the wrong
  511. * offset. There is still a race, but provided the app is using its
  512. * own file locking, this will make O_APPEND work as expected.
  513. *
  514. */
  515. static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  516. unsigned long nr_segs, loff_t pos)
  517. {
  518. struct file *file = iocb->ki_filp;
  519. if (file->f_flags & O_APPEND) {
  520. struct dentry *dentry = file->f_dentry;
  521. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  522. struct gfs2_holder gh;
  523. int ret;
  524. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  525. if (ret)
  526. return ret;
  527. gfs2_glock_dq_uninit(&gh);
  528. }
  529. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  530. }
  531. static int empty_write_end(struct page *page, unsigned from,
  532. unsigned to, int mode)
  533. {
  534. struct inode *inode = page->mapping->host;
  535. struct gfs2_inode *ip = GFS2_I(inode);
  536. struct buffer_head *bh;
  537. unsigned offset, blksize = 1 << inode->i_blkbits;
  538. pgoff_t end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  539. zero_user(page, from, to-from);
  540. mark_page_accessed(page);
  541. if (page->index < end_index || !(mode & FALLOC_FL_KEEP_SIZE)) {
  542. if (!gfs2_is_writeback(ip))
  543. gfs2_page_add_databufs(ip, page, from, to);
  544. block_commit_write(page, from, to);
  545. return 0;
  546. }
  547. offset = 0;
  548. bh = page_buffers(page);
  549. while (offset < to) {
  550. if (offset >= from) {
  551. set_buffer_uptodate(bh);
  552. mark_buffer_dirty(bh);
  553. clear_buffer_new(bh);
  554. write_dirty_buffer(bh, WRITE);
  555. }
  556. offset += blksize;
  557. bh = bh->b_this_page;
  558. }
  559. offset = 0;
  560. bh = page_buffers(page);
  561. while (offset < to) {
  562. if (offset >= from) {
  563. wait_on_buffer(bh);
  564. if (!buffer_uptodate(bh))
  565. return -EIO;
  566. }
  567. offset += blksize;
  568. bh = bh->b_this_page;
  569. }
  570. return 0;
  571. }
  572. static int needs_empty_write(sector_t block, struct inode *inode)
  573. {
  574. int error;
  575. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  576. bh_map.b_size = 1 << inode->i_blkbits;
  577. error = gfs2_block_map(inode, block, &bh_map, 0);
  578. if (unlikely(error))
  579. return error;
  580. return !buffer_mapped(&bh_map);
  581. }
  582. static int write_empty_blocks(struct page *page, unsigned from, unsigned to,
  583. int mode)
  584. {
  585. struct inode *inode = page->mapping->host;
  586. unsigned start, end, next, blksize;
  587. sector_t block = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  588. int ret;
  589. blksize = 1 << inode->i_blkbits;
  590. next = end = 0;
  591. while (next < from) {
  592. next += blksize;
  593. block++;
  594. }
  595. start = next;
  596. do {
  597. next += blksize;
  598. ret = needs_empty_write(block, inode);
  599. if (unlikely(ret < 0))
  600. return ret;
  601. if (ret == 0) {
  602. if (end) {
  603. ret = __block_write_begin(page, start, end - start,
  604. gfs2_block_map);
  605. if (unlikely(ret))
  606. return ret;
  607. ret = empty_write_end(page, start, end, mode);
  608. if (unlikely(ret))
  609. return ret;
  610. end = 0;
  611. }
  612. start = next;
  613. }
  614. else
  615. end = next;
  616. block++;
  617. } while (next < to);
  618. if (end) {
  619. ret = __block_write_begin(page, start, end - start, gfs2_block_map);
  620. if (unlikely(ret))
  621. return ret;
  622. ret = empty_write_end(page, start, end, mode);
  623. if (unlikely(ret))
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  629. int mode)
  630. {
  631. struct gfs2_inode *ip = GFS2_I(inode);
  632. struct buffer_head *dibh;
  633. int error;
  634. u64 start = offset >> PAGE_CACHE_SHIFT;
  635. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  636. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  637. pgoff_t curr;
  638. struct page *page;
  639. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  640. unsigned int from, to;
  641. if (!end_offset)
  642. end_offset = PAGE_CACHE_SIZE;
  643. error = gfs2_meta_inode_buffer(ip, &dibh);
  644. if (unlikely(error))
  645. goto out;
  646. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  647. if (gfs2_is_stuffed(ip)) {
  648. error = gfs2_unstuff_dinode(ip, NULL);
  649. if (unlikely(error))
  650. goto out;
  651. }
  652. curr = start;
  653. offset = start << PAGE_CACHE_SHIFT;
  654. from = start_offset;
  655. to = PAGE_CACHE_SIZE;
  656. while (curr <= end) {
  657. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  658. AOP_FLAG_NOFS);
  659. if (unlikely(!page)) {
  660. error = -ENOMEM;
  661. goto out;
  662. }
  663. if (curr == end)
  664. to = end_offset;
  665. error = write_empty_blocks(page, from, to, mode);
  666. if (!error && offset + to > inode->i_size &&
  667. !(mode & FALLOC_FL_KEEP_SIZE)) {
  668. i_size_write(inode, offset + to);
  669. }
  670. unlock_page(page);
  671. page_cache_release(page);
  672. if (error)
  673. goto out;
  674. curr++;
  675. offset += PAGE_CACHE_SIZE;
  676. from = 0;
  677. }
  678. gfs2_dinode_out(ip, dibh->b_data);
  679. mark_inode_dirty(inode);
  680. brelse(dibh);
  681. out:
  682. return error;
  683. }
  684. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  685. unsigned int *data_blocks, unsigned int *ind_blocks)
  686. {
  687. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  688. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  689. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  690. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  691. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  692. max_data -= tmp;
  693. }
  694. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  695. so it might end up with fewer data blocks */
  696. if (max_data <= *data_blocks)
  697. return;
  698. *data_blocks = max_data;
  699. *ind_blocks = max_blocks - max_data;
  700. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  701. if (*len > max) {
  702. *len = max;
  703. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  704. }
  705. }
  706. static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
  707. loff_t len)
  708. {
  709. struct inode *inode = file->f_path.dentry->d_inode;
  710. struct gfs2_sbd *sdp = GFS2_SB(inode);
  711. struct gfs2_inode *ip = GFS2_I(inode);
  712. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  713. loff_t bytes, max_bytes;
  714. struct gfs2_alloc *al;
  715. int error;
  716. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  717. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  718. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  719. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  720. if (mode & ~FALLOC_FL_KEEP_SIZE)
  721. return -EOPNOTSUPP;
  722. offset &= bsize_mask;
  723. len = next - offset;
  724. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  725. if (!bytes)
  726. bytes = UINT_MAX;
  727. bytes &= bsize_mask;
  728. if (bytes == 0)
  729. bytes = sdp->sd_sb.sb_bsize;
  730. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  731. error = gfs2_glock_nq(&ip->i_gh);
  732. if (unlikely(error))
  733. goto out_uninit;
  734. if (!gfs2_write_alloc_required(ip, offset, len))
  735. goto out_unlock;
  736. while (len > 0) {
  737. if (len < bytes)
  738. bytes = len;
  739. al = gfs2_alloc_get(ip);
  740. if (!al) {
  741. error = -ENOMEM;
  742. goto out_unlock;
  743. }
  744. error = gfs2_quota_lock_check(ip);
  745. if (error)
  746. goto out_alloc_put;
  747. retry:
  748. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  749. al->al_requested = data_blocks + ind_blocks;
  750. error = gfs2_inplace_reserve(ip);
  751. if (error) {
  752. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  753. bytes >>= 1;
  754. bytes &= bsize_mask;
  755. if (bytes == 0)
  756. bytes = sdp->sd_sb.sb_bsize;
  757. goto retry;
  758. }
  759. goto out_qunlock;
  760. }
  761. max_bytes = bytes;
  762. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  763. al->al_requested = data_blocks + ind_blocks;
  764. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  765. RES_RG_HDR + gfs2_rg_blocks(al);
  766. if (gfs2_is_jdata(ip))
  767. rblocks += data_blocks ? data_blocks : 1;
  768. error = gfs2_trans_begin(sdp, rblocks,
  769. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  770. if (error)
  771. goto out_trans_fail;
  772. error = fallocate_chunk(inode, offset, max_bytes, mode);
  773. gfs2_trans_end(sdp);
  774. if (error)
  775. goto out_trans_fail;
  776. len -= max_bytes;
  777. offset += max_bytes;
  778. gfs2_inplace_release(ip);
  779. gfs2_quota_unlock(ip);
  780. gfs2_alloc_put(ip);
  781. }
  782. goto out_unlock;
  783. out_trans_fail:
  784. gfs2_inplace_release(ip);
  785. out_qunlock:
  786. gfs2_quota_unlock(ip);
  787. out_alloc_put:
  788. gfs2_alloc_put(ip);
  789. out_unlock:
  790. gfs2_glock_dq(&ip->i_gh);
  791. out_uninit:
  792. gfs2_holder_uninit(&ip->i_gh);
  793. return error;
  794. }
  795. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  796. /**
  797. * gfs2_setlease - acquire/release a file lease
  798. * @file: the file pointer
  799. * @arg: lease type
  800. * @fl: file lock
  801. *
  802. * We don't currently have a way to enforce a lease across the whole
  803. * cluster; until we do, disable leases (by just returning -EINVAL),
  804. * unless the administrator has requested purely local locking.
  805. *
  806. * Locking: called under lock_flocks
  807. *
  808. * Returns: errno
  809. */
  810. static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
  811. {
  812. return -EINVAL;
  813. }
  814. /**
  815. * gfs2_lock - acquire/release a posix lock on a file
  816. * @file: the file pointer
  817. * @cmd: either modify or retrieve lock state, possibly wait
  818. * @fl: type and range of lock
  819. *
  820. * Returns: errno
  821. */
  822. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  823. {
  824. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  825. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  826. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  827. if (!(fl->fl_flags & FL_POSIX))
  828. return -ENOLCK;
  829. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  830. return -ENOLCK;
  831. if (cmd == F_CANCELLK) {
  832. /* Hack: */
  833. cmd = F_SETLK;
  834. fl->fl_type = F_UNLCK;
  835. }
  836. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  837. return -EIO;
  838. if (IS_GETLK(cmd))
  839. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  840. else if (fl->fl_type == F_UNLCK)
  841. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  842. else
  843. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  844. }
  845. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  846. {
  847. struct gfs2_file *fp = file->private_data;
  848. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  849. struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
  850. struct gfs2_glock *gl;
  851. unsigned int state;
  852. int flags;
  853. int error = 0;
  854. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  855. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
  856. mutex_lock(&fp->f_fl_mutex);
  857. gl = fl_gh->gh_gl;
  858. if (gl) {
  859. if (fl_gh->gh_state == state)
  860. goto out;
  861. flock_lock_file_wait(file,
  862. &(struct file_lock){.fl_type = F_UNLCK});
  863. gfs2_glock_dq_wait(fl_gh);
  864. gfs2_holder_reinit(state, flags, fl_gh);
  865. } else {
  866. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  867. &gfs2_flock_glops, CREATE, &gl);
  868. if (error)
  869. goto out;
  870. gfs2_holder_init(gl, state, flags, fl_gh);
  871. gfs2_glock_put(gl);
  872. }
  873. error = gfs2_glock_nq(fl_gh);
  874. if (error) {
  875. gfs2_holder_uninit(fl_gh);
  876. if (error == GLR_TRYFAILED)
  877. error = -EAGAIN;
  878. } else {
  879. error = flock_lock_file_wait(file, fl);
  880. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  881. }
  882. out:
  883. mutex_unlock(&fp->f_fl_mutex);
  884. return error;
  885. }
  886. static void do_unflock(struct file *file, struct file_lock *fl)
  887. {
  888. struct gfs2_file *fp = file->private_data;
  889. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  890. mutex_lock(&fp->f_fl_mutex);
  891. flock_lock_file_wait(file, fl);
  892. if (fl_gh->gh_gl) {
  893. gfs2_glock_dq_wait(fl_gh);
  894. gfs2_holder_uninit(fl_gh);
  895. }
  896. mutex_unlock(&fp->f_fl_mutex);
  897. }
  898. /**
  899. * gfs2_flock - acquire/release a flock lock on a file
  900. * @file: the file pointer
  901. * @cmd: either modify or retrieve lock state, possibly wait
  902. * @fl: type and range of lock
  903. *
  904. * Returns: errno
  905. */
  906. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  907. {
  908. if (!(fl->fl_flags & FL_FLOCK))
  909. return -ENOLCK;
  910. if (fl->fl_type & LOCK_MAND)
  911. return -EOPNOTSUPP;
  912. if (fl->fl_type == F_UNLCK) {
  913. do_unflock(file, fl);
  914. return 0;
  915. } else {
  916. return do_flock(file, cmd, fl);
  917. }
  918. }
  919. const struct file_operations gfs2_file_fops = {
  920. .llseek = gfs2_llseek,
  921. .read = do_sync_read,
  922. .aio_read = generic_file_aio_read,
  923. .write = do_sync_write,
  924. .aio_write = gfs2_file_aio_write,
  925. .unlocked_ioctl = gfs2_ioctl,
  926. .mmap = gfs2_mmap,
  927. .open = gfs2_open,
  928. .release = gfs2_close,
  929. .fsync = gfs2_fsync,
  930. .lock = gfs2_lock,
  931. .flock = gfs2_flock,
  932. .splice_read = generic_file_splice_read,
  933. .splice_write = generic_file_splice_write,
  934. .setlease = gfs2_setlease,
  935. .fallocate = gfs2_fallocate,
  936. };
  937. const struct file_operations gfs2_dir_fops = {
  938. .readdir = gfs2_readdir,
  939. .unlocked_ioctl = gfs2_ioctl,
  940. .open = gfs2_open,
  941. .release = gfs2_close,
  942. .fsync = gfs2_fsync,
  943. .lock = gfs2_lock,
  944. .flock = gfs2_flock,
  945. .llseek = default_llseek,
  946. };
  947. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  948. const struct file_operations gfs2_file_fops_nolock = {
  949. .llseek = gfs2_llseek,
  950. .read = do_sync_read,
  951. .aio_read = generic_file_aio_read,
  952. .write = do_sync_write,
  953. .aio_write = gfs2_file_aio_write,
  954. .unlocked_ioctl = gfs2_ioctl,
  955. .mmap = gfs2_mmap,
  956. .open = gfs2_open,
  957. .release = gfs2_close,
  958. .fsync = gfs2_fsync,
  959. .splice_read = generic_file_splice_read,
  960. .splice_write = generic_file_splice_write,
  961. .setlease = generic_setlease,
  962. .fallocate = gfs2_fallocate,
  963. };
  964. const struct file_operations gfs2_dir_fops_nolock = {
  965. .readdir = gfs2_readdir,
  966. .unlocked_ioctl = gfs2_ioctl,
  967. .open = gfs2_open,
  968. .release = gfs2_close,
  969. .fsync = gfs2_fsync,
  970. .llseek = default_llseek,
  971. };