file.c 29 KB

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