file.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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/falloc.h>
  21. #include <linux/swap.h>
  22. #include <linux/crc32.h>
  23. #include <linux/writeback.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/dlm.h>
  26. #include <linux/dlm_plock.h>
  27. #include <linux/delay.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. * @whence: 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 whence)
  53. {
  54. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  55. struct gfs2_holder i_gh;
  56. loff_t error;
  57. switch (whence) {
  58. case SEEK_END: /* These reference inode->i_size */
  59. case SEEK_DATA:
  60. case SEEK_HOLE:
  61. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  62. &i_gh);
  63. if (!error) {
  64. error = generic_file_llseek(file, offset, whence);
  65. gfs2_glock_dq_uninit(&i_gh);
  66. }
  67. break;
  68. case SEEK_CUR:
  69. case SEEK_SET:
  70. error = generic_file_llseek(file, offset, whence);
  71. break;
  72. default:
  73. error = -EINVAL;
  74. }
  75. return error;
  76. }
  77. /**
  78. * gfs2_readdir - Iterator for a directory
  79. * @file: The directory to read from
  80. * @ctx: What to feed directory entries to
  81. *
  82. * Returns: errno
  83. */
  84. static int gfs2_readdir(struct file *file, struct dir_context *ctx)
  85. {
  86. struct inode *dir = file->f_mapping->host;
  87. struct gfs2_inode *dip = GFS2_I(dir);
  88. struct gfs2_holder d_gh;
  89. int error;
  90. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  91. if (error)
  92. return error;
  93. error = gfs2_dir_read(dir, ctx, &file->f_ra);
  94. gfs2_glock_dq_uninit(&d_gh);
  95. return error;
  96. }
  97. /**
  98. * fsflags_cvt
  99. * @table: A table of 32 u32 flags
  100. * @val: a 32 bit value to convert
  101. *
  102. * This function can be used to convert between fsflags values and
  103. * GFS2's own flags values.
  104. *
  105. * Returns: the converted flags
  106. */
  107. static u32 fsflags_cvt(const u32 *table, u32 val)
  108. {
  109. u32 res = 0;
  110. while(val) {
  111. if (val & 1)
  112. res |= *table;
  113. table++;
  114. val >>= 1;
  115. }
  116. return res;
  117. }
  118. static const u32 fsflags_to_gfs2[32] = {
  119. [3] = GFS2_DIF_SYNC,
  120. [4] = GFS2_DIF_IMMUTABLE,
  121. [5] = GFS2_DIF_APPENDONLY,
  122. [7] = GFS2_DIF_NOATIME,
  123. [12] = GFS2_DIF_EXHASH,
  124. [14] = GFS2_DIF_INHERIT_JDATA,
  125. [17] = GFS2_DIF_TOPDIR,
  126. };
  127. static const u32 gfs2_to_fsflags[32] = {
  128. [gfs2fl_Sync] = FS_SYNC_FL,
  129. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  130. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  131. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  132. [gfs2fl_ExHash] = FS_INDEX_FL,
  133. [gfs2fl_TopLevel] = FS_TOPDIR_FL,
  134. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  135. };
  136. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  137. {
  138. struct inode *inode = file_inode(filp);
  139. struct gfs2_inode *ip = GFS2_I(inode);
  140. struct gfs2_holder gh;
  141. int error;
  142. u32 fsflags;
  143. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  144. error = gfs2_glock_nq(&gh);
  145. if (error)
  146. goto out_uninit;
  147. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_diskflags);
  148. if (!S_ISDIR(inode->i_mode) && ip->i_diskflags & GFS2_DIF_JDATA)
  149. fsflags |= FS_JOURNAL_DATA_FL;
  150. if (put_user(fsflags, ptr))
  151. error = -EFAULT;
  152. gfs2_glock_dq(&gh);
  153. out_uninit:
  154. gfs2_holder_uninit(&gh);
  155. return error;
  156. }
  157. void gfs2_set_inode_flags(struct inode *inode)
  158. {
  159. struct gfs2_inode *ip = GFS2_I(inode);
  160. unsigned int flags = inode->i_flags;
  161. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  162. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  163. flags |= S_NOSEC;
  164. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  165. flags |= S_IMMUTABLE;
  166. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  167. flags |= S_APPEND;
  168. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  169. flags |= S_NOATIME;
  170. if (ip->i_diskflags & GFS2_DIF_SYNC)
  171. flags |= S_SYNC;
  172. inode->i_flags = flags;
  173. }
  174. /* Flags that can be set by user space */
  175. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  176. GFS2_DIF_IMMUTABLE| \
  177. GFS2_DIF_APPENDONLY| \
  178. GFS2_DIF_NOATIME| \
  179. GFS2_DIF_SYNC| \
  180. GFS2_DIF_SYSTEM| \
  181. GFS2_DIF_TOPDIR| \
  182. GFS2_DIF_INHERIT_JDATA)
  183. /**
  184. * do_gfs2_set_flags - set flags on an inode
  185. * @filp: file pointer
  186. * @reqflags: The flags to set
  187. * @mask: Indicates which flags are valid
  188. *
  189. */
  190. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  191. {
  192. struct inode *inode = file_inode(filp);
  193. struct gfs2_inode *ip = GFS2_I(inode);
  194. struct gfs2_sbd *sdp = GFS2_SB(inode);
  195. struct buffer_head *bh;
  196. struct gfs2_holder gh;
  197. int error;
  198. u32 new_flags, flags;
  199. error = mnt_want_write_file(filp);
  200. if (error)
  201. return error;
  202. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  203. if (error)
  204. goto out_drop_write;
  205. error = -EACCES;
  206. if (!inode_owner_or_capable(inode))
  207. goto out;
  208. error = 0;
  209. flags = ip->i_diskflags;
  210. new_flags = (flags & ~mask) | (reqflags & mask);
  211. if ((new_flags ^ flags) == 0)
  212. goto out;
  213. error = -EINVAL;
  214. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  215. goto out;
  216. error = -EPERM;
  217. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  218. goto out;
  219. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  220. goto out;
  221. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  222. !capable(CAP_LINUX_IMMUTABLE))
  223. goto out;
  224. if (!IS_IMMUTABLE(inode)) {
  225. error = gfs2_permission(inode, MAY_WRITE);
  226. if (error)
  227. goto out;
  228. }
  229. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  230. if (flags & GFS2_DIF_JDATA)
  231. gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
  232. error = filemap_fdatawrite(inode->i_mapping);
  233. if (error)
  234. goto out;
  235. error = filemap_fdatawait(inode->i_mapping);
  236. if (error)
  237. goto out;
  238. }
  239. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  240. if (error)
  241. goto out;
  242. error = gfs2_meta_inode_buffer(ip, &bh);
  243. if (error)
  244. goto out_trans_end;
  245. gfs2_trans_add_meta(ip->i_gl, bh);
  246. ip->i_diskflags = new_flags;
  247. gfs2_dinode_out(ip, bh->b_data);
  248. brelse(bh);
  249. gfs2_set_inode_flags(inode);
  250. gfs2_set_aops(inode);
  251. out_trans_end:
  252. gfs2_trans_end(sdp);
  253. out:
  254. gfs2_glock_dq_uninit(&gh);
  255. out_drop_write:
  256. mnt_drop_write_file(filp);
  257. return error;
  258. }
  259. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  260. {
  261. struct inode *inode = file_inode(filp);
  262. u32 fsflags, gfsflags;
  263. if (get_user(fsflags, ptr))
  264. return -EFAULT;
  265. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  266. if (!S_ISDIR(inode->i_mode)) {
  267. gfsflags &= ~GFS2_DIF_TOPDIR;
  268. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  269. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  270. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_SYSTEM);
  271. }
  272. return do_gfs2_set_flags(filp, gfsflags, ~(GFS2_DIF_SYSTEM | GFS2_DIF_JDATA));
  273. }
  274. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  275. {
  276. switch(cmd) {
  277. case FS_IOC_GETFLAGS:
  278. return gfs2_get_flags(filp, (u32 __user *)arg);
  279. case FS_IOC_SETFLAGS:
  280. return gfs2_set_flags(filp, (u32 __user *)arg);
  281. case FITRIM:
  282. return gfs2_fitrim(filp, (void __user *)arg);
  283. }
  284. return -ENOTTY;
  285. }
  286. /**
  287. * gfs2_size_hint - Give a hint to the size of a write request
  288. * @filep: The struct file
  289. * @offset: The file offset of the write
  290. * @size: The length of the write
  291. *
  292. * When we are about to do a write, this function records the total
  293. * write size in order to provide a suitable hint to the lower layers
  294. * about how many blocks will be required.
  295. *
  296. */
  297. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  298. {
  299. struct inode *inode = file_inode(filep);
  300. struct gfs2_sbd *sdp = GFS2_SB(inode);
  301. struct gfs2_inode *ip = GFS2_I(inode);
  302. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  303. int hint = min_t(size_t, INT_MAX, blks);
  304. if (hint > atomic_read(&ip->i_res.rs_sizehint))
  305. atomic_set(&ip->i_res.rs_sizehint, hint);
  306. }
  307. /**
  308. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  309. * @page: The (locked) page to allocate backing for
  310. *
  311. * We try to allocate all the blocks required for the page in
  312. * one go. This might fail for various reasons, so we keep
  313. * trying until all the blocks to back this page are allocated.
  314. * If some of the blocks are already allocated, thats ok too.
  315. */
  316. static int gfs2_allocate_page_backing(struct page *page)
  317. {
  318. struct inode *inode = page->mapping->host;
  319. struct buffer_head bh;
  320. unsigned long size = PAGE_SIZE;
  321. u64 lblock = page->index << (PAGE_SHIFT - inode->i_blkbits);
  322. do {
  323. bh.b_state = 0;
  324. bh.b_size = size;
  325. gfs2_block_map(inode, lblock, &bh, 1);
  326. if (!buffer_mapped(&bh))
  327. return -EIO;
  328. size -= bh.b_size;
  329. lblock += (bh.b_size >> inode->i_blkbits);
  330. } while(size > 0);
  331. return 0;
  332. }
  333. /**
  334. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  335. * @vma: The virtual memory area
  336. * @vmf: The virtual memory fault containing the page to become writable
  337. *
  338. * When the page becomes writable, we need to ensure that we have
  339. * blocks allocated on disk to back that page.
  340. */
  341. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  342. {
  343. struct page *page = vmf->page;
  344. struct inode *inode = file_inode(vma->vm_file);
  345. struct gfs2_inode *ip = GFS2_I(inode);
  346. struct gfs2_sbd *sdp = GFS2_SB(inode);
  347. struct gfs2_alloc_parms ap = { .aflags = 0, };
  348. unsigned long last_index;
  349. u64 pos = page->index << PAGE_SHIFT;
  350. unsigned int data_blocks, ind_blocks, rblocks;
  351. struct gfs2_holder gh;
  352. loff_t size;
  353. int ret;
  354. sb_start_pagefault(inode->i_sb);
  355. ret = gfs2_rsqa_alloc(ip);
  356. if (ret)
  357. goto out;
  358. gfs2_size_hint(vma->vm_file, pos, PAGE_SIZE);
  359. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  360. ret = gfs2_glock_nq(&gh);
  361. if (ret)
  362. goto out_uninit;
  363. /* Update file times before taking page lock */
  364. file_update_time(vma->vm_file);
  365. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  366. set_bit(GIF_SW_PAGED, &ip->i_flags);
  367. if (!gfs2_write_alloc_required(ip, pos, PAGE_SIZE)) {
  368. lock_page(page);
  369. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  370. ret = -EAGAIN;
  371. unlock_page(page);
  372. }
  373. goto out_unlock;
  374. }
  375. ret = gfs2_rindex_update(sdp);
  376. if (ret)
  377. goto out_unlock;
  378. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  379. ap.target = data_blocks + ind_blocks;
  380. ret = gfs2_quota_lock_check(ip, &ap);
  381. if (ret)
  382. goto out_unlock;
  383. ret = gfs2_inplace_reserve(ip, &ap);
  384. if (ret)
  385. goto out_quota_unlock;
  386. rblocks = RES_DINODE + ind_blocks;
  387. if (gfs2_is_jdata(ip))
  388. rblocks += data_blocks ? data_blocks : 1;
  389. if (ind_blocks || data_blocks) {
  390. rblocks += RES_STATFS + RES_QUOTA;
  391. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  392. }
  393. ret = gfs2_trans_begin(sdp, rblocks, 0);
  394. if (ret)
  395. goto out_trans_fail;
  396. lock_page(page);
  397. ret = -EINVAL;
  398. size = i_size_read(inode);
  399. last_index = (size - 1) >> PAGE_SHIFT;
  400. /* Check page index against inode size */
  401. if (size == 0 || (page->index > last_index))
  402. goto out_trans_end;
  403. ret = -EAGAIN;
  404. /* If truncated, we must retry the operation, we may have raced
  405. * with the glock demotion code.
  406. */
  407. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  408. goto out_trans_end;
  409. /* Unstuff, if required, and allocate backing blocks for page */
  410. ret = 0;
  411. if (gfs2_is_stuffed(ip))
  412. ret = gfs2_unstuff_dinode(ip, page);
  413. if (ret == 0)
  414. ret = gfs2_allocate_page_backing(page);
  415. out_trans_end:
  416. if (ret)
  417. unlock_page(page);
  418. gfs2_trans_end(sdp);
  419. out_trans_fail:
  420. gfs2_inplace_release(ip);
  421. out_quota_unlock:
  422. gfs2_quota_unlock(ip);
  423. out_unlock:
  424. gfs2_glock_dq(&gh);
  425. out_uninit:
  426. gfs2_holder_uninit(&gh);
  427. if (ret == 0) {
  428. set_page_dirty(page);
  429. wait_for_stable_page(page);
  430. }
  431. out:
  432. sb_end_pagefault(inode->i_sb);
  433. return block_page_mkwrite_return(ret);
  434. }
  435. static const struct vm_operations_struct gfs2_vm_ops = {
  436. .fault = filemap_fault,
  437. .map_pages = filemap_map_pages,
  438. .page_mkwrite = gfs2_page_mkwrite,
  439. };
  440. /**
  441. * gfs2_mmap -
  442. * @file: The file to map
  443. * @vma: The VMA which described the mapping
  444. *
  445. * There is no need to get a lock here unless we should be updating
  446. * atime. We ignore any locking errors since the only consequence is
  447. * a missed atime update (which will just be deferred until later).
  448. *
  449. * Returns: 0
  450. */
  451. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  452. {
  453. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  454. if (!(file->f_flags & O_NOATIME) &&
  455. !IS_NOATIME(&ip->i_inode)) {
  456. struct gfs2_holder i_gh;
  457. int error;
  458. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  459. &i_gh);
  460. if (error)
  461. return error;
  462. /* grab lock to update inode */
  463. gfs2_glock_dq_uninit(&i_gh);
  464. file_accessed(file);
  465. }
  466. vma->vm_ops = &gfs2_vm_ops;
  467. return 0;
  468. }
  469. /**
  470. * gfs2_open_common - This is common to open and atomic_open
  471. * @inode: The inode being opened
  472. * @file: The file being opened
  473. *
  474. * This maybe called under a glock or not depending upon how it has
  475. * been called. We must always be called under a glock for regular
  476. * files, however. For other file types, it does not matter whether
  477. * we hold the glock or not.
  478. *
  479. * Returns: Error code or 0 for success
  480. */
  481. int gfs2_open_common(struct inode *inode, struct file *file)
  482. {
  483. struct gfs2_file *fp;
  484. int ret;
  485. if (S_ISREG(inode->i_mode)) {
  486. ret = generic_file_open(inode, file);
  487. if (ret)
  488. return ret;
  489. }
  490. fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
  491. if (!fp)
  492. return -ENOMEM;
  493. mutex_init(&fp->f_fl_mutex);
  494. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  495. file->private_data = fp;
  496. return 0;
  497. }
  498. /**
  499. * gfs2_open - open a file
  500. * @inode: the inode to open
  501. * @file: the struct file for this opening
  502. *
  503. * After atomic_open, this function is only used for opening files
  504. * which are already cached. We must still get the glock for regular
  505. * files to ensure that we have the file size uptodate for the large
  506. * file check which is in the common code. That is only an issue for
  507. * regular files though.
  508. *
  509. * Returns: errno
  510. */
  511. static int gfs2_open(struct inode *inode, struct file *file)
  512. {
  513. struct gfs2_inode *ip = GFS2_I(inode);
  514. struct gfs2_holder i_gh;
  515. int error;
  516. bool need_unlock = false;
  517. if (S_ISREG(ip->i_inode.i_mode)) {
  518. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  519. &i_gh);
  520. if (error)
  521. return error;
  522. need_unlock = true;
  523. }
  524. error = gfs2_open_common(inode, file);
  525. if (need_unlock)
  526. gfs2_glock_dq_uninit(&i_gh);
  527. return error;
  528. }
  529. /**
  530. * gfs2_release - called to close a struct file
  531. * @inode: the inode the struct file belongs to
  532. * @file: the struct file being closed
  533. *
  534. * Returns: errno
  535. */
  536. static int gfs2_release(struct inode *inode, struct file *file)
  537. {
  538. struct gfs2_inode *ip = GFS2_I(inode);
  539. kfree(file->private_data);
  540. file->private_data = NULL;
  541. if (!(file->f_mode & FMODE_WRITE))
  542. return 0;
  543. gfs2_rsqa_delete(ip, &inode->i_writecount);
  544. return 0;
  545. }
  546. /**
  547. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  548. * @file: the file that points to the dentry
  549. * @start: the start position in the file to sync
  550. * @end: the end position in the file to sync
  551. * @datasync: set if we can ignore timestamp changes
  552. *
  553. * We split the data flushing here so that we don't wait for the data
  554. * until after we've also sent the metadata to disk. Note that for
  555. * data=ordered, we will write & wait for the data at the log flush
  556. * stage anyway, so this is unlikely to make much of a difference
  557. * except in the data=writeback case.
  558. *
  559. * If the fdatawrite fails due to any reason except -EIO, we will
  560. * continue the remainder of the fsync, although we'll still report
  561. * the error at the end. This is to match filemap_write_and_wait_range()
  562. * behaviour.
  563. *
  564. * Returns: errno
  565. */
  566. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  567. int datasync)
  568. {
  569. struct address_space *mapping = file->f_mapping;
  570. struct inode *inode = mapping->host;
  571. int sync_state = inode->i_state & I_DIRTY_ALL;
  572. struct gfs2_inode *ip = GFS2_I(inode);
  573. int ret = 0, ret1 = 0;
  574. if (mapping->nrpages) {
  575. ret1 = filemap_fdatawrite_range(mapping, start, end);
  576. if (ret1 == -EIO)
  577. return ret1;
  578. }
  579. if (!gfs2_is_jdata(ip))
  580. sync_state &= ~I_DIRTY_PAGES;
  581. if (datasync)
  582. sync_state &= ~(I_DIRTY_SYNC | I_DIRTY_TIME);
  583. if (sync_state) {
  584. ret = sync_inode_metadata(inode, 1);
  585. if (ret)
  586. return ret;
  587. if (gfs2_is_jdata(ip))
  588. filemap_write_and_wait(mapping);
  589. gfs2_ail_flush(ip->i_gl, 1);
  590. }
  591. if (mapping->nrpages)
  592. ret = filemap_fdatawait_range(mapping, start, end);
  593. return ret ? ret : ret1;
  594. }
  595. /**
  596. * gfs2_file_write_iter - Perform a write to a file
  597. * @iocb: The io context
  598. * @iov: The data to write
  599. * @nr_segs: Number of @iov segments
  600. * @pos: The file position
  601. *
  602. * We have to do a lock/unlock here to refresh the inode size for
  603. * O_APPEND writes, otherwise we can land up writing at the wrong
  604. * offset. There is still a race, but provided the app is using its
  605. * own file locking, this will make O_APPEND work as expected.
  606. *
  607. */
  608. static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  609. {
  610. struct file *file = iocb->ki_filp;
  611. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  612. int ret;
  613. ret = gfs2_rsqa_alloc(ip);
  614. if (ret)
  615. return ret;
  616. gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
  617. if (iocb->ki_flags & IOCB_APPEND) {
  618. struct gfs2_holder gh;
  619. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  620. if (ret)
  621. return ret;
  622. gfs2_glock_dq_uninit(&gh);
  623. }
  624. return generic_file_write_iter(iocb, from);
  625. }
  626. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  627. int mode)
  628. {
  629. struct gfs2_inode *ip = GFS2_I(inode);
  630. struct buffer_head *dibh;
  631. int error;
  632. unsigned int nr_blks;
  633. sector_t lblock = offset >> inode->i_blkbits;
  634. error = gfs2_meta_inode_buffer(ip, &dibh);
  635. if (unlikely(error))
  636. return error;
  637. gfs2_trans_add_meta(ip->i_gl, dibh);
  638. if (gfs2_is_stuffed(ip)) {
  639. error = gfs2_unstuff_dinode(ip, NULL);
  640. if (unlikely(error))
  641. goto out;
  642. }
  643. while (len) {
  644. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  645. bh_map.b_size = len;
  646. set_buffer_zeronew(&bh_map);
  647. error = gfs2_block_map(inode, lblock, &bh_map, 1);
  648. if (unlikely(error))
  649. goto out;
  650. len -= bh_map.b_size;
  651. nr_blks = bh_map.b_size >> inode->i_blkbits;
  652. lblock += nr_blks;
  653. if (!buffer_new(&bh_map))
  654. continue;
  655. if (unlikely(!buffer_zeronew(&bh_map))) {
  656. error = -EIO;
  657. goto out;
  658. }
  659. }
  660. out:
  661. brelse(dibh);
  662. return error;
  663. }
  664. /**
  665. * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
  666. * blocks, determine how many bytes can be written.
  667. * @ip: The inode in question.
  668. * @len: Max cap of bytes. What we return in *len must be <= this.
  669. * @data_blocks: Compute and return the number of data blocks needed
  670. * @ind_blocks: Compute and return the number of indirect blocks needed
  671. * @max_blocks: The total blocks available to work with.
  672. *
  673. * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
  674. */
  675. static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
  676. unsigned int *data_blocks, unsigned int *ind_blocks,
  677. unsigned int max_blocks)
  678. {
  679. loff_t max = *len;
  680. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  681. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  682. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  683. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  684. max_data -= tmp;
  685. }
  686. *data_blocks = max_data;
  687. *ind_blocks = max_blocks - max_data;
  688. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  689. if (*len > max) {
  690. *len = max;
  691. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  692. }
  693. }
  694. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  695. {
  696. struct inode *inode = file_inode(file);
  697. struct gfs2_sbd *sdp = GFS2_SB(inode);
  698. struct gfs2_inode *ip = GFS2_I(inode);
  699. struct gfs2_alloc_parms ap = { .aflags = 0, };
  700. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  701. loff_t bytes, max_bytes, max_blks = UINT_MAX;
  702. int error;
  703. const loff_t pos = offset;
  704. const loff_t count = len;
  705. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  706. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  707. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  708. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  709. offset &= bsize_mask;
  710. len = next - offset;
  711. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  712. if (!bytes)
  713. bytes = UINT_MAX;
  714. bytes &= bsize_mask;
  715. if (bytes == 0)
  716. bytes = sdp->sd_sb.sb_bsize;
  717. gfs2_size_hint(file, offset, len);
  718. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  719. ap.min_target = data_blocks + ind_blocks;
  720. while (len > 0) {
  721. if (len < bytes)
  722. bytes = len;
  723. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  724. len -= bytes;
  725. offset += bytes;
  726. continue;
  727. }
  728. /* We need to determine how many bytes we can actually
  729. * fallocate without exceeding quota or going over the
  730. * end of the fs. We start off optimistically by assuming
  731. * we can write max_bytes */
  732. max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
  733. /* Since max_bytes is most likely a theoretical max, we
  734. * calculate a more realistic 'bytes' to serve as a good
  735. * starting point for the number of bytes we may be able
  736. * to write */
  737. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  738. ap.target = data_blocks + ind_blocks;
  739. error = gfs2_quota_lock_check(ip, &ap);
  740. if (error)
  741. return error;
  742. /* ap.allowed tells us how many blocks quota will allow
  743. * us to write. Check if this reduces max_blks */
  744. if (ap.allowed && ap.allowed < max_blks)
  745. max_blks = ap.allowed;
  746. error = gfs2_inplace_reserve(ip, &ap);
  747. if (error)
  748. goto out_qunlock;
  749. /* check if the selected rgrp limits our max_blks further */
  750. if (ap.allowed && ap.allowed < max_blks)
  751. max_blks = ap.allowed;
  752. /* Almost done. Calculate bytes that can be written using
  753. * max_blks. We also recompute max_bytes, data_blocks and
  754. * ind_blocks */
  755. calc_max_reserv(ip, &max_bytes, &data_blocks,
  756. &ind_blocks, max_blks);
  757. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  758. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  759. if (gfs2_is_jdata(ip))
  760. rblocks += data_blocks ? data_blocks : 1;
  761. error = gfs2_trans_begin(sdp, rblocks,
  762. PAGE_SIZE/sdp->sd_sb.sb_bsize);
  763. if (error)
  764. goto out_trans_fail;
  765. error = fallocate_chunk(inode, offset, max_bytes, mode);
  766. gfs2_trans_end(sdp);
  767. if (error)
  768. goto out_trans_fail;
  769. len -= max_bytes;
  770. offset += max_bytes;
  771. gfs2_inplace_release(ip);
  772. gfs2_quota_unlock(ip);
  773. }
  774. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size) {
  775. i_size_write(inode, pos + count);
  776. file_update_time(file);
  777. mark_inode_dirty(inode);
  778. }
  779. if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
  780. return vfs_fsync_range(file, pos, pos + count - 1,
  781. (file->f_flags & __O_SYNC) ? 0 : 1);
  782. return 0;
  783. out_trans_fail:
  784. gfs2_inplace_release(ip);
  785. out_qunlock:
  786. gfs2_quota_unlock(ip);
  787. return error;
  788. }
  789. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  790. {
  791. struct inode *inode = file_inode(file);
  792. struct gfs2_inode *ip = GFS2_I(inode);
  793. struct gfs2_holder gh;
  794. int ret;
  795. if ((mode & ~FALLOC_FL_KEEP_SIZE) || gfs2_is_jdata(ip))
  796. return -EOPNOTSUPP;
  797. inode_lock(inode);
  798. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  799. ret = gfs2_glock_nq(&gh);
  800. if (ret)
  801. goto out_uninit;
  802. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  803. (offset + len) > inode->i_size) {
  804. ret = inode_newsize_ok(inode, offset + len);
  805. if (ret)
  806. goto out_unlock;
  807. }
  808. ret = get_write_access(inode);
  809. if (ret)
  810. goto out_unlock;
  811. ret = gfs2_rsqa_alloc(ip);
  812. if (ret)
  813. goto out_putw;
  814. ret = __gfs2_fallocate(file, mode, offset, len);
  815. if (ret)
  816. gfs2_rs_deltree(&ip->i_res);
  817. out_putw:
  818. put_write_access(inode);
  819. out_unlock:
  820. gfs2_glock_dq(&gh);
  821. out_uninit:
  822. gfs2_holder_uninit(&gh);
  823. inode_unlock(inode);
  824. return ret;
  825. }
  826. static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
  827. struct file *out, loff_t *ppos,
  828. size_t len, unsigned int flags)
  829. {
  830. int error;
  831. struct gfs2_inode *ip = GFS2_I(out->f_mapping->host);
  832. error = gfs2_rsqa_alloc(ip);
  833. if (error)
  834. return (ssize_t)error;
  835. gfs2_size_hint(out, *ppos, len);
  836. return iter_file_splice_write(pipe, out, ppos, len, flags);
  837. }
  838. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  839. /**
  840. * gfs2_lock - acquire/release a posix lock on a file
  841. * @file: the file pointer
  842. * @cmd: either modify or retrieve lock state, possibly wait
  843. * @fl: type and range of lock
  844. *
  845. * Returns: errno
  846. */
  847. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  848. {
  849. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  850. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  851. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  852. if (!(fl->fl_flags & FL_POSIX))
  853. return -ENOLCK;
  854. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  855. return -ENOLCK;
  856. if (cmd == F_CANCELLK) {
  857. /* Hack: */
  858. cmd = F_SETLK;
  859. fl->fl_type = F_UNLCK;
  860. }
  861. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  862. if (fl->fl_type == F_UNLCK)
  863. locks_lock_file_wait(file, fl);
  864. return -EIO;
  865. }
  866. if (IS_GETLK(cmd))
  867. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  868. else if (fl->fl_type == F_UNLCK)
  869. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  870. else
  871. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  872. }
  873. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  874. {
  875. struct gfs2_file *fp = file->private_data;
  876. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  877. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  878. struct gfs2_glock *gl;
  879. unsigned int state;
  880. u16 flags;
  881. int error = 0;
  882. int sleeptime;
  883. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  884. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
  885. mutex_lock(&fp->f_fl_mutex);
  886. gl = fl_gh->gh_gl;
  887. if (gl) {
  888. if (fl_gh->gh_state == state)
  889. goto out;
  890. locks_lock_file_wait(file,
  891. &(struct file_lock) {
  892. .fl_type = F_UNLCK,
  893. .fl_flags = FL_FLOCK
  894. });
  895. gfs2_glock_dq(fl_gh);
  896. gfs2_holder_reinit(state, flags, fl_gh);
  897. } else {
  898. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  899. &gfs2_flock_glops, CREATE, &gl);
  900. if (error)
  901. goto out;
  902. gfs2_holder_init(gl, state, flags, fl_gh);
  903. gfs2_glock_put(gl);
  904. }
  905. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  906. error = gfs2_glock_nq(fl_gh);
  907. if (error != GLR_TRYFAILED)
  908. break;
  909. fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
  910. fl_gh->gh_error = 0;
  911. msleep(sleeptime);
  912. }
  913. if (error) {
  914. gfs2_holder_uninit(fl_gh);
  915. if (error == GLR_TRYFAILED)
  916. error = -EAGAIN;
  917. } else {
  918. error = locks_lock_file_wait(file, fl);
  919. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  920. }
  921. out:
  922. mutex_unlock(&fp->f_fl_mutex);
  923. return error;
  924. }
  925. static void do_unflock(struct file *file, struct file_lock *fl)
  926. {
  927. struct gfs2_file *fp = file->private_data;
  928. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  929. mutex_lock(&fp->f_fl_mutex);
  930. locks_lock_file_wait(file, fl);
  931. if (gfs2_holder_initialized(fl_gh)) {
  932. gfs2_glock_dq(fl_gh);
  933. gfs2_holder_uninit(fl_gh);
  934. }
  935. mutex_unlock(&fp->f_fl_mutex);
  936. }
  937. /**
  938. * gfs2_flock - acquire/release a flock lock on a file
  939. * @file: the file pointer
  940. * @cmd: either modify or retrieve lock state, possibly wait
  941. * @fl: type and range of lock
  942. *
  943. * Returns: errno
  944. */
  945. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  946. {
  947. if (!(fl->fl_flags & FL_FLOCK))
  948. return -ENOLCK;
  949. if (fl->fl_type & LOCK_MAND)
  950. return -EOPNOTSUPP;
  951. if (fl->fl_type == F_UNLCK) {
  952. do_unflock(file, fl);
  953. return 0;
  954. } else {
  955. return do_flock(file, cmd, fl);
  956. }
  957. }
  958. const struct file_operations gfs2_file_fops = {
  959. .llseek = gfs2_llseek,
  960. .read_iter = generic_file_read_iter,
  961. .write_iter = gfs2_file_write_iter,
  962. .unlocked_ioctl = gfs2_ioctl,
  963. .mmap = gfs2_mmap,
  964. .open = gfs2_open,
  965. .release = gfs2_release,
  966. .fsync = gfs2_fsync,
  967. .lock = gfs2_lock,
  968. .flock = gfs2_flock,
  969. .splice_read = generic_file_splice_read,
  970. .splice_write = gfs2_file_splice_write,
  971. .setlease = simple_nosetlease,
  972. .fallocate = gfs2_fallocate,
  973. };
  974. const struct file_operations gfs2_dir_fops = {
  975. .iterate_shared = gfs2_readdir,
  976. .unlocked_ioctl = gfs2_ioctl,
  977. .open = gfs2_open,
  978. .release = gfs2_release,
  979. .fsync = gfs2_fsync,
  980. .lock = gfs2_lock,
  981. .flock = gfs2_flock,
  982. .llseek = default_llseek,
  983. };
  984. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  985. const struct file_operations gfs2_file_fops_nolock = {
  986. .llseek = gfs2_llseek,
  987. .read_iter = generic_file_read_iter,
  988. .write_iter = gfs2_file_write_iter,
  989. .unlocked_ioctl = gfs2_ioctl,
  990. .mmap = gfs2_mmap,
  991. .open = gfs2_open,
  992. .release = gfs2_release,
  993. .fsync = gfs2_fsync,
  994. .splice_read = generic_file_splice_read,
  995. .splice_write = gfs2_file_splice_write,
  996. .setlease = generic_setlease,
  997. .fallocate = gfs2_fallocate,
  998. };
  999. const struct file_operations gfs2_dir_fops_nolock = {
  1000. .iterate_shared = gfs2_readdir,
  1001. .unlocked_ioctl = gfs2_ioctl,
  1002. .open = gfs2_open,
  1003. .release = gfs2_release,
  1004. .fsync = gfs2_fsync,
  1005. .llseek = default_llseek,
  1006. };