file.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. 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. 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. * do_gfs2_set_flags - set flags on an inode
  184. * @filp: file pointer
  185. * @reqflags: 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 (new_flags & GFS2_DIF_JDATA)
  230. gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
  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. if (new_flags & GFS2_DIF_JDATA)
  238. gfs2_ordered_del_inode(ip);
  239. }
  240. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  241. if (error)
  242. goto out;
  243. error = gfs2_meta_inode_buffer(ip, &bh);
  244. if (error)
  245. goto out_trans_end;
  246. gfs2_trans_add_meta(ip->i_gl, bh);
  247. ip->i_diskflags = new_flags;
  248. gfs2_dinode_out(ip, bh->b_data);
  249. brelse(bh);
  250. gfs2_set_inode_flags(inode);
  251. gfs2_set_aops(inode);
  252. out_trans_end:
  253. gfs2_trans_end(sdp);
  254. out:
  255. gfs2_glock_dq_uninit(&gh);
  256. out_drop_write:
  257. mnt_drop_write_file(filp);
  258. return error;
  259. }
  260. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  261. {
  262. struct inode *inode = file_inode(filp);
  263. u32 fsflags, gfsflags;
  264. if (get_user(fsflags, ptr))
  265. return -EFAULT;
  266. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  267. if (!S_ISDIR(inode->i_mode)) {
  268. gfsflags &= ~GFS2_DIF_TOPDIR;
  269. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  270. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  271. return do_gfs2_set_flags(filp, gfsflags, ~0);
  272. }
  273. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  274. }
  275. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  276. {
  277. switch(cmd) {
  278. case FS_IOC_GETFLAGS:
  279. return gfs2_get_flags(filp, (u32 __user *)arg);
  280. case FS_IOC_SETFLAGS:
  281. return gfs2_set_flags(filp, (u32 __user *)arg);
  282. case FITRIM:
  283. return gfs2_fitrim(filp, (void __user *)arg);
  284. }
  285. return -ENOTTY;
  286. }
  287. /**
  288. * gfs2_size_hint - Give a hint to the size of a write request
  289. * @filep: The struct file
  290. * @offset: The file offset of the write
  291. * @size: The length of the write
  292. *
  293. * When we are about to do a write, this function records the total
  294. * write size in order to provide a suitable hint to the lower layers
  295. * about how many blocks will be required.
  296. *
  297. */
  298. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  299. {
  300. struct inode *inode = file_inode(filep);
  301. struct gfs2_sbd *sdp = GFS2_SB(inode);
  302. struct gfs2_inode *ip = GFS2_I(inode);
  303. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  304. int hint = min_t(size_t, INT_MAX, blks);
  305. if (hint > atomic_read(&ip->i_res->rs_sizehint))
  306. atomic_set(&ip->i_res->rs_sizehint, hint);
  307. }
  308. /**
  309. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  310. * @page: The (locked) page to allocate backing for
  311. *
  312. * We try to allocate all the blocks required for the page in
  313. * one go. This might fail for various reasons, so we keep
  314. * trying until all the blocks to back this page are allocated.
  315. * If some of the blocks are already allocated, thats ok too.
  316. */
  317. static int gfs2_allocate_page_backing(struct page *page)
  318. {
  319. struct inode *inode = page->mapping->host;
  320. struct buffer_head bh;
  321. unsigned long size = PAGE_CACHE_SIZE;
  322. u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  323. do {
  324. bh.b_state = 0;
  325. bh.b_size = size;
  326. gfs2_block_map(inode, lblock, &bh, 1);
  327. if (!buffer_mapped(&bh))
  328. return -EIO;
  329. size -= bh.b_size;
  330. lblock += (bh.b_size >> inode->i_blkbits);
  331. } while(size > 0);
  332. return 0;
  333. }
  334. /**
  335. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  336. * @vma: The virtual memory area
  337. * @vmf: The virtual memory fault containing the page to become writable
  338. *
  339. * When the page becomes writable, we need to ensure that we have
  340. * blocks allocated on disk to back that page.
  341. */
  342. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  343. {
  344. struct page *page = vmf->page;
  345. struct inode *inode = file_inode(vma->vm_file);
  346. struct gfs2_inode *ip = GFS2_I(inode);
  347. struct gfs2_sbd *sdp = GFS2_SB(inode);
  348. struct gfs2_alloc_parms ap = { .aflags = 0, };
  349. unsigned long last_index;
  350. u64 pos = page->index << PAGE_CACHE_SHIFT;
  351. unsigned int data_blocks, ind_blocks, rblocks;
  352. struct gfs2_holder gh;
  353. loff_t size;
  354. int ret;
  355. sb_start_pagefault(inode->i_sb);
  356. /* Update file times before taking page lock */
  357. file_update_time(vma->vm_file);
  358. ret = get_write_access(inode);
  359. if (ret)
  360. goto out;
  361. ret = gfs2_rs_alloc(ip);
  362. if (ret)
  363. goto out_write_access;
  364. gfs2_size_hint(vma->vm_file, pos, PAGE_CACHE_SIZE);
  365. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  366. ret = gfs2_glock_nq(&gh);
  367. if (ret)
  368. goto out_uninit;
  369. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  370. set_bit(GIF_SW_PAGED, &ip->i_flags);
  371. if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
  372. lock_page(page);
  373. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  374. ret = -EAGAIN;
  375. unlock_page(page);
  376. }
  377. goto out_unlock;
  378. }
  379. ret = gfs2_rindex_update(sdp);
  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_quota_lock_check(ip, &ap);
  385. if (ret)
  386. goto out_unlock;
  387. ret = gfs2_inplace_reserve(ip, &ap);
  388. if (ret)
  389. goto out_quota_unlock;
  390. rblocks = RES_DINODE + ind_blocks;
  391. if (gfs2_is_jdata(ip))
  392. rblocks += data_blocks ? data_blocks : 1;
  393. if (ind_blocks || data_blocks) {
  394. rblocks += RES_STATFS + RES_QUOTA;
  395. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  396. }
  397. ret = gfs2_trans_begin(sdp, rblocks, 0);
  398. if (ret)
  399. goto out_trans_fail;
  400. lock_page(page);
  401. ret = -EINVAL;
  402. size = i_size_read(inode);
  403. last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  404. /* Check page index against inode size */
  405. if (size == 0 || (page->index > last_index))
  406. goto out_trans_end;
  407. ret = -EAGAIN;
  408. /* If truncated, we must retry the operation, we may have raced
  409. * with the glock demotion code.
  410. */
  411. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  412. goto out_trans_end;
  413. /* Unstuff, if required, and allocate backing blocks for page */
  414. ret = 0;
  415. if (gfs2_is_stuffed(ip))
  416. ret = gfs2_unstuff_dinode(ip, page);
  417. if (ret == 0)
  418. ret = gfs2_allocate_page_backing(page);
  419. out_trans_end:
  420. if (ret)
  421. unlock_page(page);
  422. gfs2_trans_end(sdp);
  423. out_trans_fail:
  424. gfs2_inplace_release(ip);
  425. out_quota_unlock:
  426. gfs2_quota_unlock(ip);
  427. out_unlock:
  428. gfs2_glock_dq(&gh);
  429. out_uninit:
  430. gfs2_holder_uninit(&gh);
  431. if (ret == 0) {
  432. set_page_dirty(page);
  433. wait_for_stable_page(page);
  434. }
  435. out_write_access:
  436. put_write_access(inode);
  437. out:
  438. sb_end_pagefault(inode->i_sb);
  439. return block_page_mkwrite_return(ret);
  440. }
  441. static const struct vm_operations_struct gfs2_vm_ops = {
  442. .fault = filemap_fault,
  443. .map_pages = filemap_map_pages,
  444. .page_mkwrite = gfs2_page_mkwrite,
  445. };
  446. /**
  447. * gfs2_mmap -
  448. * @file: The file to map
  449. * @vma: The VMA which described the mapping
  450. *
  451. * There is no need to get a lock here unless we should be updating
  452. * atime. We ignore any locking errors since the only consequence is
  453. * a missed atime update (which will just be deferred until later).
  454. *
  455. * Returns: 0
  456. */
  457. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  458. {
  459. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  460. if (!(file->f_flags & O_NOATIME) &&
  461. !IS_NOATIME(&ip->i_inode)) {
  462. struct gfs2_holder i_gh;
  463. int error;
  464. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  465. &i_gh);
  466. if (error)
  467. return error;
  468. /* grab lock to update inode */
  469. gfs2_glock_dq_uninit(&i_gh);
  470. file_accessed(file);
  471. }
  472. vma->vm_ops = &gfs2_vm_ops;
  473. return 0;
  474. }
  475. /**
  476. * gfs2_open_common - This is common to open and atomic_open
  477. * @inode: The inode being opened
  478. * @file: The file being opened
  479. *
  480. * This maybe called under a glock or not depending upon how it has
  481. * been called. We must always be called under a glock for regular
  482. * files, however. For other file types, it does not matter whether
  483. * we hold the glock or not.
  484. *
  485. * Returns: Error code or 0 for success
  486. */
  487. int gfs2_open_common(struct inode *inode, struct file *file)
  488. {
  489. struct gfs2_file *fp;
  490. int ret;
  491. if (S_ISREG(inode->i_mode)) {
  492. ret = generic_file_open(inode, file);
  493. if (ret)
  494. return ret;
  495. }
  496. fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
  497. if (!fp)
  498. return -ENOMEM;
  499. mutex_init(&fp->f_fl_mutex);
  500. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  501. file->private_data = fp;
  502. return 0;
  503. }
  504. /**
  505. * gfs2_open - open a file
  506. * @inode: the inode to open
  507. * @file: the struct file for this opening
  508. *
  509. * After atomic_open, this function is only used for opening files
  510. * which are already cached. We must still get the glock for regular
  511. * files to ensure that we have the file size uptodate for the large
  512. * file check which is in the common code. That is only an issue for
  513. * regular files though.
  514. *
  515. * Returns: errno
  516. */
  517. static int gfs2_open(struct inode *inode, struct file *file)
  518. {
  519. struct gfs2_inode *ip = GFS2_I(inode);
  520. struct gfs2_holder i_gh;
  521. int error;
  522. bool need_unlock = false;
  523. if (S_ISREG(ip->i_inode.i_mode)) {
  524. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  525. &i_gh);
  526. if (error)
  527. return error;
  528. need_unlock = true;
  529. }
  530. error = gfs2_open_common(inode, file);
  531. if (need_unlock)
  532. gfs2_glock_dq_uninit(&i_gh);
  533. return error;
  534. }
  535. /**
  536. * gfs2_release - called to close a struct file
  537. * @inode: the inode the struct file belongs to
  538. * @file: the struct file being closed
  539. *
  540. * Returns: errno
  541. */
  542. static int gfs2_release(struct inode *inode, struct file *file)
  543. {
  544. struct gfs2_inode *ip = GFS2_I(inode);
  545. kfree(file->private_data);
  546. file->private_data = NULL;
  547. if (!(file->f_mode & FMODE_WRITE))
  548. return 0;
  549. gfs2_rs_delete(ip, &inode->i_writecount);
  550. return 0;
  551. }
  552. /**
  553. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  554. * @file: the file that points to the dentry
  555. * @start: the start position in the file to sync
  556. * @end: the end position in the file to sync
  557. * @datasync: set if we can ignore timestamp changes
  558. *
  559. * We split the data flushing here so that we don't wait for the data
  560. * until after we've also sent the metadata to disk. Note that for
  561. * data=ordered, we will write & wait for the data at the log flush
  562. * stage anyway, so this is unlikely to make much of a difference
  563. * except in the data=writeback case.
  564. *
  565. * If the fdatawrite fails due to any reason except -EIO, we will
  566. * continue the remainder of the fsync, although we'll still report
  567. * the error at the end. This is to match filemap_write_and_wait_range()
  568. * behaviour.
  569. *
  570. * Returns: errno
  571. */
  572. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  573. int datasync)
  574. {
  575. struct address_space *mapping = file->f_mapping;
  576. struct inode *inode = mapping->host;
  577. int sync_state = inode->i_state & I_DIRTY_ALL;
  578. struct gfs2_inode *ip = GFS2_I(inode);
  579. int ret = 0, ret1 = 0;
  580. if (mapping->nrpages) {
  581. ret1 = filemap_fdatawrite_range(mapping, start, end);
  582. if (ret1 == -EIO)
  583. return ret1;
  584. }
  585. if (!gfs2_is_jdata(ip))
  586. sync_state &= ~I_DIRTY_PAGES;
  587. if (datasync)
  588. sync_state &= ~(I_DIRTY_SYNC | I_DIRTY_TIME);
  589. if (sync_state) {
  590. ret = sync_inode_metadata(inode, 1);
  591. if (ret)
  592. return ret;
  593. if (gfs2_is_jdata(ip))
  594. filemap_write_and_wait(mapping);
  595. gfs2_ail_flush(ip->i_gl, 1);
  596. }
  597. if (mapping->nrpages)
  598. ret = filemap_fdatawait_range(mapping, start, end);
  599. return ret ? ret : ret1;
  600. }
  601. /**
  602. * gfs2_file_write_iter - Perform a write to a file
  603. * @iocb: The io context
  604. * @iov: The data to write
  605. * @nr_segs: Number of @iov segments
  606. * @pos: The file position
  607. *
  608. * We have to do a lock/unlock here to refresh the inode size for
  609. * O_APPEND writes, otherwise we can land up writing at the wrong
  610. * offset. There is still a race, but provided the app is using its
  611. * own file locking, this will make O_APPEND work as expected.
  612. *
  613. */
  614. static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  615. {
  616. struct file *file = iocb->ki_filp;
  617. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  618. int ret;
  619. ret = gfs2_rs_alloc(ip);
  620. if (ret)
  621. return ret;
  622. gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
  623. if (iocb->ki_flags & IOCB_APPEND) {
  624. struct gfs2_holder gh;
  625. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  626. if (ret)
  627. return ret;
  628. gfs2_glock_dq_uninit(&gh);
  629. }
  630. return generic_file_write_iter(iocb, from);
  631. }
  632. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  633. int mode)
  634. {
  635. struct gfs2_inode *ip = GFS2_I(inode);
  636. struct buffer_head *dibh;
  637. int error;
  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. out:
  667. brelse(dibh);
  668. return error;
  669. }
  670. /**
  671. * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
  672. * blocks, determine how many bytes can be written.
  673. * @ip: The inode in question.
  674. * @len: Max cap of bytes. What we return in *len must be <= this.
  675. * @data_blocks: Compute and return the number of data blocks needed
  676. * @ind_blocks: Compute and return the number of indirect blocks needed
  677. * @max_blocks: The total blocks available to work with.
  678. *
  679. * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
  680. */
  681. static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
  682. unsigned int *data_blocks, unsigned int *ind_blocks,
  683. unsigned int max_blocks)
  684. {
  685. loff_t max = *len;
  686. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  687. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  688. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  689. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  690. max_data -= tmp;
  691. }
  692. *data_blocks = max_data;
  693. *ind_blocks = max_blocks - max_data;
  694. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  695. if (*len > max) {
  696. *len = max;
  697. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  698. }
  699. }
  700. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  701. {
  702. struct inode *inode = file_inode(file);
  703. struct gfs2_sbd *sdp = GFS2_SB(inode);
  704. struct gfs2_inode *ip = GFS2_I(inode);
  705. struct gfs2_alloc_parms ap = { .aflags = 0, };
  706. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  707. loff_t bytes, max_bytes, max_blks;
  708. int error;
  709. const loff_t pos = offset;
  710. const loff_t count = len;
  711. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  712. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  713. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  714. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  715. offset &= bsize_mask;
  716. len = next - offset;
  717. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  718. if (!bytes)
  719. bytes = UINT_MAX;
  720. bytes &= bsize_mask;
  721. if (bytes == 0)
  722. bytes = sdp->sd_sb.sb_bsize;
  723. gfs2_size_hint(file, offset, len);
  724. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  725. ap.min_target = data_blocks + ind_blocks;
  726. while (len > 0) {
  727. if (len < bytes)
  728. bytes = len;
  729. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  730. len -= bytes;
  731. offset += bytes;
  732. continue;
  733. }
  734. /* We need to determine how many bytes we can actually
  735. * fallocate without exceeding quota or going over the
  736. * end of the fs. We start off optimistically by assuming
  737. * we can write max_bytes */
  738. max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
  739. /* Since max_bytes is most likely a theoretical max, we
  740. * calculate a more realistic 'bytes' to serve as a good
  741. * starting point for the number of bytes we may be able
  742. * to write */
  743. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  744. ap.target = data_blocks + ind_blocks;
  745. error = gfs2_quota_lock_check(ip, &ap);
  746. if (error)
  747. return error;
  748. /* ap.allowed tells us how many blocks quota will allow
  749. * us to write. Check if this reduces max_blks */
  750. max_blks = UINT_MAX;
  751. if (ap.allowed)
  752. max_blks = ap.allowed;
  753. error = gfs2_inplace_reserve(ip, &ap);
  754. if (error)
  755. goto out_qunlock;
  756. /* check if the selected rgrp limits our max_blks further */
  757. if (ap.allowed && ap.allowed < max_blks)
  758. max_blks = ap.allowed;
  759. /* Almost done. Calculate bytes that can be written using
  760. * max_blks. We also recompute max_bytes, data_blocks and
  761. * ind_blocks */
  762. calc_max_reserv(ip, &max_bytes, &data_blocks,
  763. &ind_blocks, max_blks);
  764. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  765. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  766. if (gfs2_is_jdata(ip))
  767. rblocks += data_blocks ? data_blocks : 1;
  768. error = gfs2_trans_begin(sdp, rblocks,
  769. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  770. if (error)
  771. goto out_trans_fail;
  772. error = fallocate_chunk(inode, offset, max_bytes, mode);
  773. gfs2_trans_end(sdp);
  774. if (error)
  775. goto out_trans_fail;
  776. len -= max_bytes;
  777. offset += max_bytes;
  778. gfs2_inplace_release(ip);
  779. gfs2_quota_unlock(ip);
  780. }
  781. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size) {
  782. i_size_write(inode, pos + count);
  783. file_update_time(file);
  784. mark_inode_dirty(inode);
  785. }
  786. return generic_write_sync(file, pos, count);
  787. out_trans_fail:
  788. gfs2_inplace_release(ip);
  789. out_qunlock:
  790. gfs2_quota_unlock(ip);
  791. return error;
  792. }
  793. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  794. {
  795. struct inode *inode = file_inode(file);
  796. struct gfs2_inode *ip = GFS2_I(inode);
  797. struct gfs2_holder gh;
  798. int ret;
  799. if ((mode & ~FALLOC_FL_KEEP_SIZE) || gfs2_is_jdata(ip))
  800. return -EOPNOTSUPP;
  801. mutex_lock(&inode->i_mutex);
  802. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  803. ret = gfs2_glock_nq(&gh);
  804. if (ret)
  805. goto out_uninit;
  806. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  807. (offset + len) > inode->i_size) {
  808. ret = inode_newsize_ok(inode, offset + len);
  809. if (ret)
  810. goto out_unlock;
  811. }
  812. ret = get_write_access(inode);
  813. if (ret)
  814. goto out_unlock;
  815. ret = gfs2_rs_alloc(ip);
  816. if (ret)
  817. goto out_putw;
  818. ret = __gfs2_fallocate(file, mode, offset, len);
  819. if (ret)
  820. gfs2_rs_deltree(ip->i_res);
  821. out_putw:
  822. put_write_access(inode);
  823. out_unlock:
  824. gfs2_glock_dq(&gh);
  825. out_uninit:
  826. gfs2_holder_uninit(&gh);
  827. mutex_unlock(&inode->i_mutex);
  828. return ret;
  829. }
  830. static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
  831. struct file *out, loff_t *ppos,
  832. size_t len, unsigned int flags)
  833. {
  834. int error;
  835. struct gfs2_inode *ip = GFS2_I(out->f_mapping->host);
  836. error = gfs2_rs_alloc(ip);
  837. if (error)
  838. return (ssize_t)error;
  839. gfs2_size_hint(out, *ppos, len);
  840. return iter_file_splice_write(pipe, out, ppos, len, flags);
  841. }
  842. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  843. /**
  844. * gfs2_lock - acquire/release a posix lock on a file
  845. * @file: the file pointer
  846. * @cmd: either modify or retrieve lock state, possibly wait
  847. * @fl: type and range of lock
  848. *
  849. * Returns: errno
  850. */
  851. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  852. {
  853. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  854. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  855. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  856. if (!(fl->fl_flags & FL_POSIX))
  857. return -ENOLCK;
  858. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  859. return -ENOLCK;
  860. if (cmd == F_CANCELLK) {
  861. /* Hack: */
  862. cmd = F_SETLK;
  863. fl->fl_type = F_UNLCK;
  864. }
  865. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  866. if (fl->fl_type == F_UNLCK)
  867. locks_lock_file_wait(file, fl);
  868. return -EIO;
  869. }
  870. if (IS_GETLK(cmd))
  871. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  872. else if (fl->fl_type == F_UNLCK)
  873. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  874. else
  875. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  876. }
  877. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  878. {
  879. struct gfs2_file *fp = file->private_data;
  880. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  881. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  882. struct gfs2_glock *gl;
  883. unsigned int state;
  884. int flags;
  885. int error = 0;
  886. int sleeptime;
  887. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  888. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
  889. mutex_lock(&fp->f_fl_mutex);
  890. gl = fl_gh->gh_gl;
  891. if (gl) {
  892. if (fl_gh->gh_state == state)
  893. goto out;
  894. locks_lock_file_wait(file,
  895. &(struct file_lock){.fl_type = F_UNLCK});
  896. gfs2_glock_dq(fl_gh);
  897. gfs2_holder_reinit(state, flags, fl_gh);
  898. } else {
  899. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  900. &gfs2_flock_glops, CREATE, &gl);
  901. if (error)
  902. goto out;
  903. gfs2_holder_init(gl, state, flags, fl_gh);
  904. gfs2_glock_put(gl);
  905. }
  906. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  907. error = gfs2_glock_nq(fl_gh);
  908. if (error != GLR_TRYFAILED)
  909. break;
  910. fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
  911. fl_gh->gh_error = 0;
  912. msleep(sleeptime);
  913. }
  914. if (error) {
  915. gfs2_holder_uninit(fl_gh);
  916. if (error == GLR_TRYFAILED)
  917. error = -EAGAIN;
  918. } else {
  919. error = locks_lock_file_wait(file, fl);
  920. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  921. }
  922. out:
  923. mutex_unlock(&fp->f_fl_mutex);
  924. return error;
  925. }
  926. static void do_unflock(struct file *file, struct file_lock *fl)
  927. {
  928. struct gfs2_file *fp = file->private_data;
  929. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  930. mutex_lock(&fp->f_fl_mutex);
  931. locks_lock_file_wait(file, fl);
  932. if (fl_gh->gh_gl) {
  933. gfs2_glock_dq(fl_gh);
  934. gfs2_holder_uninit(fl_gh);
  935. }
  936. mutex_unlock(&fp->f_fl_mutex);
  937. }
  938. /**
  939. * gfs2_flock - acquire/release a flock lock on a file
  940. * @file: the file pointer
  941. * @cmd: either modify or retrieve lock state, possibly wait
  942. * @fl: type and range of lock
  943. *
  944. * Returns: errno
  945. */
  946. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  947. {
  948. if (!(fl->fl_flags & FL_FLOCK))
  949. return -ENOLCK;
  950. if (fl->fl_type & LOCK_MAND)
  951. return -EOPNOTSUPP;
  952. if (fl->fl_type == F_UNLCK) {
  953. do_unflock(file, fl);
  954. return 0;
  955. } else {
  956. return do_flock(file, cmd, fl);
  957. }
  958. }
  959. const struct file_operations gfs2_file_fops = {
  960. .llseek = gfs2_llseek,
  961. .read_iter = generic_file_read_iter,
  962. .write_iter = gfs2_file_write_iter,
  963. .unlocked_ioctl = gfs2_ioctl,
  964. .mmap = gfs2_mmap,
  965. .open = gfs2_open,
  966. .release = gfs2_release,
  967. .fsync = gfs2_fsync,
  968. .lock = gfs2_lock,
  969. .flock = gfs2_flock,
  970. .splice_read = generic_file_splice_read,
  971. .splice_write = gfs2_file_splice_write,
  972. .setlease = simple_nosetlease,
  973. .fallocate = gfs2_fallocate,
  974. };
  975. const struct file_operations gfs2_dir_fops = {
  976. .iterate = gfs2_readdir,
  977. .unlocked_ioctl = gfs2_ioctl,
  978. .open = gfs2_open,
  979. .release = gfs2_release,
  980. .fsync = gfs2_fsync,
  981. .lock = gfs2_lock,
  982. .flock = gfs2_flock,
  983. .llseek = default_llseek,
  984. };
  985. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  986. const struct file_operations gfs2_file_fops_nolock = {
  987. .llseek = gfs2_llseek,
  988. .read_iter = generic_file_read_iter,
  989. .write_iter = gfs2_file_write_iter,
  990. .unlocked_ioctl = gfs2_ioctl,
  991. .mmap = gfs2_mmap,
  992. .open = gfs2_open,
  993. .release = gfs2_release,
  994. .fsync = gfs2_fsync,
  995. .splice_read = generic_file_splice_read,
  996. .splice_write = gfs2_file_splice_write,
  997. .setlease = generic_setlease,
  998. .fallocate = gfs2_fallocate,
  999. };
  1000. const struct file_operations gfs2_dir_fops_nolock = {
  1001. .iterate = gfs2_readdir,
  1002. .unlocked_ioctl = gfs2_ioctl,
  1003. .open = gfs2_open,
  1004. .release = gfs2_release,
  1005. .fsync = gfs2_fsync,
  1006. .llseek = default_llseek,
  1007. };