file.c 27 KB

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