file.c 27 KB

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