file.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 <linux/delay.h>
  29. #include "gfs2.h"
  30. #include "incore.h"
  31. #include "bmap.h"
  32. #include "dir.h"
  33. #include "glock.h"
  34. #include "glops.h"
  35. #include "inode.h"
  36. #include "log.h"
  37. #include "meta_io.h"
  38. #include "quota.h"
  39. #include "rgrp.h"
  40. #include "trans.h"
  41. #include "util.h"
  42. /**
  43. * gfs2_llseek - seek to a location in a file
  44. * @file: the file
  45. * @offset: the offset
  46. * @whence: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  47. *
  48. * SEEK_END requires the glock for the file because it references the
  49. * file's size.
  50. *
  51. * Returns: The new offset, or errno
  52. */
  53. static loff_t gfs2_llseek(struct file *file, loff_t offset, int whence)
  54. {
  55. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  56. struct gfs2_holder i_gh;
  57. loff_t error;
  58. switch (whence) {
  59. case SEEK_END: /* These reference inode->i_size */
  60. case SEEK_DATA:
  61. case SEEK_HOLE:
  62. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  63. &i_gh);
  64. if (!error) {
  65. error = generic_file_llseek(file, offset, whence);
  66. gfs2_glock_dq_uninit(&i_gh);
  67. }
  68. break;
  69. case SEEK_CUR:
  70. case SEEK_SET:
  71. error = generic_file_llseek(file, offset, whence);
  72. break;
  73. default:
  74. error = -EINVAL;
  75. }
  76. return error;
  77. }
  78. /**
  79. * gfs2_readdir - Iterator for a directory
  80. * @file: The directory to read from
  81. * @ctx: What to feed directory entries to
  82. *
  83. * Returns: errno
  84. */
  85. static int gfs2_readdir(struct file *file, struct dir_context *ctx)
  86. {
  87. struct inode *dir = file->f_mapping->host;
  88. struct gfs2_inode *dip = GFS2_I(dir);
  89. struct gfs2_holder d_gh;
  90. int error;
  91. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  92. if (error)
  93. return error;
  94. error = gfs2_dir_read(dir, ctx, &file->f_ra);
  95. gfs2_glock_dq_uninit(&d_gh);
  96. return error;
  97. }
  98. /**
  99. * fsflags_cvt
  100. * @table: A table of 32 u32 flags
  101. * @val: a 32 bit value to convert
  102. *
  103. * This function can be used to convert between fsflags values and
  104. * GFS2's own flags values.
  105. *
  106. * Returns: the converted flags
  107. */
  108. static u32 fsflags_cvt(const u32 *table, u32 val)
  109. {
  110. u32 res = 0;
  111. while(val) {
  112. if (val & 1)
  113. res |= *table;
  114. table++;
  115. val >>= 1;
  116. }
  117. return res;
  118. }
  119. static const u32 fsflags_to_gfs2[32] = {
  120. [3] = GFS2_DIF_SYNC,
  121. [4] = GFS2_DIF_IMMUTABLE,
  122. [5] = GFS2_DIF_APPENDONLY,
  123. [7] = GFS2_DIF_NOATIME,
  124. [12] = GFS2_DIF_EXHASH,
  125. [14] = GFS2_DIF_INHERIT_JDATA,
  126. [17] = GFS2_DIF_TOPDIR,
  127. };
  128. static const u32 gfs2_to_fsflags[32] = {
  129. [gfs2fl_Sync] = FS_SYNC_FL,
  130. [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
  131. [gfs2fl_AppendOnly] = FS_APPEND_FL,
  132. [gfs2fl_NoAtime] = FS_NOATIME_FL,
  133. [gfs2fl_ExHash] = FS_INDEX_FL,
  134. [gfs2fl_TopLevel] = FS_TOPDIR_FL,
  135. [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
  136. };
  137. static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
  138. {
  139. struct inode *inode = file_inode(filp);
  140. struct gfs2_inode *ip = GFS2_I(inode);
  141. struct gfs2_holder gh;
  142. int error;
  143. u32 fsflags;
  144. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  145. error = gfs2_glock_nq(&gh);
  146. if (error)
  147. return error;
  148. fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_diskflags);
  149. if (!S_ISDIR(inode->i_mode) && ip->i_diskflags & GFS2_DIF_JDATA)
  150. fsflags |= FS_JOURNAL_DATA_FL;
  151. if (put_user(fsflags, ptr))
  152. error = -EFAULT;
  153. gfs2_glock_dq(&gh);
  154. gfs2_holder_uninit(&gh);
  155. return error;
  156. }
  157. void gfs2_set_inode_flags(struct inode *inode)
  158. {
  159. struct gfs2_inode *ip = GFS2_I(inode);
  160. unsigned int flags = inode->i_flags;
  161. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  162. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  163. inode->i_flags |= S_NOSEC;
  164. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  165. flags |= S_IMMUTABLE;
  166. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  167. flags |= S_APPEND;
  168. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  169. flags |= S_NOATIME;
  170. if (ip->i_diskflags & GFS2_DIF_SYNC)
  171. flags |= S_SYNC;
  172. inode->i_flags = flags;
  173. }
  174. /* Flags that can be set by user space */
  175. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  176. GFS2_DIF_IMMUTABLE| \
  177. GFS2_DIF_APPENDONLY| \
  178. GFS2_DIF_NOATIME| \
  179. GFS2_DIF_SYNC| \
  180. GFS2_DIF_SYSTEM| \
  181. GFS2_DIF_TOPDIR| \
  182. GFS2_DIF_INHERIT_JDATA)
  183. /**
  184. * do_gfs2_set_flags - set flags on an inode
  185. * @filp: file pointer
  186. * @reqflags: The flags to set
  187. * @mask: Indicates which flags are valid
  188. *
  189. */
  190. static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
  191. {
  192. struct inode *inode = file_inode(filp);
  193. struct gfs2_inode *ip = GFS2_I(inode);
  194. struct gfs2_sbd *sdp = GFS2_SB(inode);
  195. struct buffer_head *bh;
  196. struct gfs2_holder gh;
  197. int error;
  198. u32 new_flags, flags;
  199. error = mnt_want_write_file(filp);
  200. if (error)
  201. return error;
  202. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  203. if (error)
  204. goto out_drop_write;
  205. error = -EACCES;
  206. if (!inode_owner_or_capable(inode))
  207. goto out;
  208. error = 0;
  209. flags = ip->i_diskflags;
  210. new_flags = (flags & ~mask) | (reqflags & mask);
  211. if ((new_flags ^ flags) == 0)
  212. goto out;
  213. error = -EINVAL;
  214. if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
  215. goto out;
  216. error = -EPERM;
  217. if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
  218. goto out;
  219. if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
  220. goto out;
  221. if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
  222. !capable(CAP_LINUX_IMMUTABLE))
  223. goto out;
  224. if (!IS_IMMUTABLE(inode)) {
  225. error = gfs2_permission(inode, MAY_WRITE);
  226. if (error)
  227. goto out;
  228. }
  229. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  230. if (flags & GFS2_DIF_JDATA)
  231. gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
  232. error = filemap_fdatawrite(inode->i_mapping);
  233. if (error)
  234. goto out;
  235. error = filemap_fdatawait(inode->i_mapping);
  236. if (error)
  237. goto out;
  238. }
  239. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  240. if (error)
  241. goto out;
  242. error = gfs2_meta_inode_buffer(ip, &bh);
  243. if (error)
  244. goto out_trans_end;
  245. gfs2_trans_add_meta(ip->i_gl, bh);
  246. ip->i_diskflags = new_flags;
  247. gfs2_dinode_out(ip, bh->b_data);
  248. brelse(bh);
  249. gfs2_set_inode_flags(inode);
  250. gfs2_set_aops(inode);
  251. out_trans_end:
  252. gfs2_trans_end(sdp);
  253. out:
  254. gfs2_glock_dq_uninit(&gh);
  255. out_drop_write:
  256. mnt_drop_write_file(filp);
  257. return error;
  258. }
  259. static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
  260. {
  261. struct inode *inode = file_inode(filp);
  262. u32 fsflags, gfsflags;
  263. if (get_user(fsflags, ptr))
  264. return -EFAULT;
  265. gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
  266. if (!S_ISDIR(inode->i_mode)) {
  267. gfsflags &= ~GFS2_DIF_TOPDIR;
  268. if (gfsflags & GFS2_DIF_INHERIT_JDATA)
  269. gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
  270. return do_gfs2_set_flags(filp, gfsflags, ~0);
  271. }
  272. return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
  273. }
  274. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  275. {
  276. switch(cmd) {
  277. case FS_IOC_GETFLAGS:
  278. return gfs2_get_flags(filp, (u32 __user *)arg);
  279. case FS_IOC_SETFLAGS:
  280. return gfs2_set_flags(filp, (u32 __user *)arg);
  281. case FITRIM:
  282. return gfs2_fitrim(filp, (void __user *)arg);
  283. }
  284. return -ENOTTY;
  285. }
  286. /**
  287. * gfs2_size_hint - Give a hint to the size of a write request
  288. * @filep: The struct file
  289. * @offset: The file offset of the write
  290. * @size: The length of the write
  291. *
  292. * When we are about to do a write, this function records the total
  293. * write size in order to provide a suitable hint to the lower layers
  294. * about how many blocks will be required.
  295. *
  296. */
  297. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  298. {
  299. struct inode *inode = file_inode(filep);
  300. struct gfs2_sbd *sdp = GFS2_SB(inode);
  301. struct gfs2_inode *ip = GFS2_I(inode);
  302. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  303. int hint = min_t(size_t, INT_MAX, blks);
  304. if (hint > atomic_read(&ip->i_res->rs_sizehint))
  305. atomic_set(&ip->i_res->rs_sizehint, hint);
  306. }
  307. /**
  308. * gfs2_allocate_page_backing - Use bmap to allocate blocks
  309. * @page: The (locked) page to allocate backing for
  310. *
  311. * We try to allocate all the blocks required for the page in
  312. * one go. This might fail for various reasons, so we keep
  313. * trying until all the blocks to back this page are allocated.
  314. * If some of the blocks are already allocated, thats ok too.
  315. */
  316. static int gfs2_allocate_page_backing(struct page *page)
  317. {
  318. struct inode *inode = page->mapping->host;
  319. struct buffer_head bh;
  320. unsigned long size = PAGE_CACHE_SIZE;
  321. u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  322. do {
  323. bh.b_state = 0;
  324. bh.b_size = size;
  325. gfs2_block_map(inode, lblock, &bh, 1);
  326. if (!buffer_mapped(&bh))
  327. return -EIO;
  328. size -= bh.b_size;
  329. lblock += (bh.b_size >> inode->i_blkbits);
  330. } while(size > 0);
  331. return 0;
  332. }
  333. /**
  334. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  335. * @vma: The virtual memory area
  336. * @vmf: The virtual memory fault containing the page to become writable
  337. *
  338. * When the page becomes writable, we need to ensure that we have
  339. * blocks allocated on disk to back that page.
  340. */
  341. static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  342. {
  343. struct page *page = vmf->page;
  344. struct inode *inode = file_inode(vma->vm_file);
  345. struct gfs2_inode *ip = GFS2_I(inode);
  346. struct gfs2_sbd *sdp = GFS2_SB(inode);
  347. struct gfs2_alloc_parms ap = { .aflags = 0, };
  348. unsigned long last_index;
  349. u64 pos = page->index << PAGE_CACHE_SHIFT;
  350. unsigned int data_blocks, ind_blocks, rblocks;
  351. struct gfs2_holder gh;
  352. loff_t size;
  353. int ret;
  354. sb_start_pagefault(inode->i_sb);
  355. /* Update file times before taking page lock */
  356. file_update_time(vma->vm_file);
  357. ret = get_write_access(inode);
  358. if (ret)
  359. goto out;
  360. ret = gfs2_rs_alloc(ip);
  361. if (ret)
  362. goto out_write_access;
  363. gfs2_size_hint(vma->vm_file, pos, PAGE_CACHE_SIZE);
  364. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  365. ret = gfs2_glock_nq(&gh);
  366. if (ret)
  367. goto out_uninit;
  368. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  369. set_bit(GIF_SW_PAGED, &ip->i_flags);
  370. if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
  371. lock_page(page);
  372. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  373. ret = -EAGAIN;
  374. unlock_page(page);
  375. }
  376. goto out_unlock;
  377. }
  378. ret = gfs2_rindex_update(sdp);
  379. if (ret)
  380. goto out_unlock;
  381. ret = gfs2_quota_lock_check(ip);
  382. if (ret)
  383. goto out_unlock;
  384. gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
  385. ap.target = data_blocks + ind_blocks;
  386. ret = gfs2_inplace_reserve(ip, &ap);
  387. if (ret)
  388. goto out_quota_unlock;
  389. rblocks = RES_DINODE + ind_blocks;
  390. if (gfs2_is_jdata(ip))
  391. rblocks += data_blocks ? data_blocks : 1;
  392. if (ind_blocks || data_blocks) {
  393. rblocks += RES_STATFS + RES_QUOTA;
  394. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  395. }
  396. ret = gfs2_trans_begin(sdp, rblocks, 0);
  397. if (ret)
  398. goto out_trans_fail;
  399. lock_page(page);
  400. ret = -EINVAL;
  401. size = i_size_read(inode);
  402. last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  403. /* Check page index against inode size */
  404. if (size == 0 || (page->index > last_index))
  405. goto out_trans_end;
  406. ret = -EAGAIN;
  407. /* If truncated, we must retry the operation, we may have raced
  408. * with the glock demotion code.
  409. */
  410. if (!PageUptodate(page) || page->mapping != inode->i_mapping)
  411. goto out_trans_end;
  412. /* Unstuff, if required, and allocate backing blocks for page */
  413. ret = 0;
  414. if (gfs2_is_stuffed(ip))
  415. ret = gfs2_unstuff_dinode(ip, page);
  416. if (ret == 0)
  417. ret = gfs2_allocate_page_backing(page);
  418. out_trans_end:
  419. if (ret)
  420. unlock_page(page);
  421. gfs2_trans_end(sdp);
  422. out_trans_fail:
  423. gfs2_inplace_release(ip);
  424. out_quota_unlock:
  425. gfs2_quota_unlock(ip);
  426. out_unlock:
  427. gfs2_glock_dq(&gh);
  428. out_uninit:
  429. gfs2_holder_uninit(&gh);
  430. if (ret == 0) {
  431. set_page_dirty(page);
  432. wait_for_stable_page(page);
  433. }
  434. out_write_access:
  435. put_write_access(inode);
  436. out:
  437. sb_end_pagefault(inode->i_sb);
  438. return block_page_mkwrite_return(ret);
  439. }
  440. static const struct vm_operations_struct gfs2_vm_ops = {
  441. .fault = filemap_fault,
  442. .map_pages = filemap_map_pages,
  443. .page_mkwrite = gfs2_page_mkwrite,
  444. .remap_pages = generic_file_remap_pages,
  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;
  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;
  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 (file->f_flags & O_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. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  671. unsigned int *data_blocks, unsigned int *ind_blocks)
  672. {
  673. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  674. unsigned int max_blocks = ip->i_rgd->rd_free_clone;
  675. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  676. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  677. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  678. max_data -= tmp;
  679. }
  680. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  681. so it might end up with fewer data blocks */
  682. if (max_data <= *data_blocks)
  683. return;
  684. *data_blocks = max_data;
  685. *ind_blocks = max_blocks - max_data;
  686. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  687. if (*len > max) {
  688. *len = max;
  689. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  690. }
  691. }
  692. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  693. {
  694. struct inode *inode = file_inode(file);
  695. struct gfs2_sbd *sdp = GFS2_SB(inode);
  696. struct gfs2_inode *ip = GFS2_I(inode);
  697. struct gfs2_alloc_parms ap = { .aflags = 0, };
  698. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  699. loff_t bytes, max_bytes;
  700. int error;
  701. const loff_t pos = offset;
  702. const loff_t count = len;
  703. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  704. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  705. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  706. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  707. offset &= bsize_mask;
  708. len = next - offset;
  709. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  710. if (!bytes)
  711. bytes = UINT_MAX;
  712. bytes &= bsize_mask;
  713. if (bytes == 0)
  714. bytes = sdp->sd_sb.sb_bsize;
  715. gfs2_size_hint(file, offset, len);
  716. while (len > 0) {
  717. if (len < bytes)
  718. bytes = len;
  719. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  720. len -= bytes;
  721. offset += bytes;
  722. continue;
  723. }
  724. error = gfs2_quota_lock_check(ip);
  725. if (error)
  726. return error;
  727. retry:
  728. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  729. ap.target = data_blocks + ind_blocks;
  730. error = gfs2_inplace_reserve(ip, &ap);
  731. if (error) {
  732. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  733. bytes >>= 1;
  734. bytes &= bsize_mask;
  735. if (bytes == 0)
  736. bytes = sdp->sd_sb.sb_bsize;
  737. goto retry;
  738. }
  739. goto out_qunlock;
  740. }
  741. max_bytes = bytes;
  742. calc_max_reserv(ip, (len > max_chunk_size)? max_chunk_size: len,
  743. &max_bytes, &data_blocks, &ind_blocks);
  744. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  745. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  746. if (gfs2_is_jdata(ip))
  747. rblocks += data_blocks ? data_blocks : 1;
  748. error = gfs2_trans_begin(sdp, rblocks,
  749. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  750. if (error)
  751. goto out_trans_fail;
  752. error = fallocate_chunk(inode, offset, max_bytes, mode);
  753. gfs2_trans_end(sdp);
  754. if (error)
  755. goto out_trans_fail;
  756. len -= max_bytes;
  757. offset += max_bytes;
  758. gfs2_inplace_release(ip);
  759. gfs2_quota_unlock(ip);
  760. }
  761. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size) {
  762. i_size_write(inode, pos + count);
  763. /* Marks the inode as dirty */
  764. file_update_time(file);
  765. }
  766. return generic_write_sync(file, pos, count);
  767. out_trans_fail:
  768. gfs2_inplace_release(ip);
  769. out_qunlock:
  770. gfs2_quota_unlock(ip);
  771. return error;
  772. }
  773. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  774. {
  775. struct inode *inode = file_inode(file);
  776. struct gfs2_inode *ip = GFS2_I(inode);
  777. struct gfs2_holder gh;
  778. int ret;
  779. if (mode & ~FALLOC_FL_KEEP_SIZE)
  780. return -EOPNOTSUPP;
  781. mutex_lock(&inode->i_mutex);
  782. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  783. ret = gfs2_glock_nq(&gh);
  784. if (ret)
  785. goto out_uninit;
  786. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  787. (offset + len) > inode->i_size) {
  788. ret = inode_newsize_ok(inode, offset + len);
  789. if (ret)
  790. goto out_unlock;
  791. }
  792. ret = get_write_access(inode);
  793. if (ret)
  794. goto out_unlock;
  795. ret = gfs2_rs_alloc(ip);
  796. if (ret)
  797. goto out_putw;
  798. ret = __gfs2_fallocate(file, mode, offset, len);
  799. if (ret)
  800. gfs2_rs_deltree(ip->i_res);
  801. out_putw:
  802. put_write_access(inode);
  803. out_unlock:
  804. gfs2_glock_dq(&gh);
  805. out_uninit:
  806. gfs2_holder_uninit(&gh);
  807. mutex_unlock(&inode->i_mutex);
  808. return ret;
  809. }
  810. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  811. /**
  812. * gfs2_lock - acquire/release a posix lock on a file
  813. * @file: the file pointer
  814. * @cmd: either modify or retrieve lock state, possibly wait
  815. * @fl: type and range of lock
  816. *
  817. * Returns: errno
  818. */
  819. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  820. {
  821. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  822. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  823. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  824. if (!(fl->fl_flags & FL_POSIX))
  825. return -ENOLCK;
  826. if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
  827. return -ENOLCK;
  828. if (cmd == F_CANCELLK) {
  829. /* Hack: */
  830. cmd = F_SETLK;
  831. fl->fl_type = F_UNLCK;
  832. }
  833. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  834. if (fl->fl_type == F_UNLCK)
  835. posix_lock_file_wait(file, fl);
  836. return -EIO;
  837. }
  838. if (IS_GETLK(cmd))
  839. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  840. else if (fl->fl_type == F_UNLCK)
  841. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  842. else
  843. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  844. }
  845. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  846. {
  847. struct gfs2_file *fp = file->private_data;
  848. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  849. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  850. struct gfs2_glock *gl;
  851. unsigned int state;
  852. int flags;
  853. int error = 0;
  854. int sleeptime;
  855. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  856. flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
  857. mutex_lock(&fp->f_fl_mutex);
  858. gl = fl_gh->gh_gl;
  859. if (gl) {
  860. if (fl_gh->gh_state == state)
  861. goto out;
  862. flock_lock_file_wait(file,
  863. &(struct file_lock){.fl_type = F_UNLCK});
  864. gfs2_glock_dq(fl_gh);
  865. gfs2_holder_reinit(state, flags, fl_gh);
  866. } else {
  867. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  868. &gfs2_flock_glops, CREATE, &gl);
  869. if (error)
  870. goto out;
  871. gfs2_holder_init(gl, state, flags, fl_gh);
  872. gfs2_glock_put(gl);
  873. }
  874. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  875. error = gfs2_glock_nq(fl_gh);
  876. if (error != GLR_TRYFAILED)
  877. break;
  878. fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
  879. fl_gh->gh_error = 0;
  880. msleep(sleeptime);
  881. }
  882. if (error) {
  883. gfs2_holder_uninit(fl_gh);
  884. if (error == GLR_TRYFAILED)
  885. error = -EAGAIN;
  886. } else {
  887. error = flock_lock_file_wait(file, fl);
  888. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  889. }
  890. out:
  891. mutex_unlock(&fp->f_fl_mutex);
  892. return error;
  893. }
  894. static void do_unflock(struct file *file, struct file_lock *fl)
  895. {
  896. struct gfs2_file *fp = file->private_data;
  897. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  898. mutex_lock(&fp->f_fl_mutex);
  899. flock_lock_file_wait(file, fl);
  900. if (fl_gh->gh_gl) {
  901. gfs2_glock_dq(fl_gh);
  902. gfs2_holder_uninit(fl_gh);
  903. }
  904. mutex_unlock(&fp->f_fl_mutex);
  905. }
  906. /**
  907. * gfs2_flock - acquire/release a flock lock on a file
  908. * @file: the file pointer
  909. * @cmd: either modify or retrieve lock state, possibly wait
  910. * @fl: type and range of lock
  911. *
  912. * Returns: errno
  913. */
  914. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  915. {
  916. if (!(fl->fl_flags & FL_FLOCK))
  917. return -ENOLCK;
  918. if (fl->fl_type & LOCK_MAND)
  919. return -EOPNOTSUPP;
  920. if (fl->fl_type == F_UNLCK) {
  921. do_unflock(file, fl);
  922. return 0;
  923. } else {
  924. return do_flock(file, cmd, fl);
  925. }
  926. }
  927. const struct file_operations gfs2_file_fops = {
  928. .llseek = gfs2_llseek,
  929. .read = new_sync_read,
  930. .read_iter = generic_file_read_iter,
  931. .write = new_sync_write,
  932. .write_iter = gfs2_file_write_iter,
  933. .unlocked_ioctl = gfs2_ioctl,
  934. .mmap = gfs2_mmap,
  935. .open = gfs2_open,
  936. .release = gfs2_release,
  937. .fsync = gfs2_fsync,
  938. .lock = gfs2_lock,
  939. .flock = gfs2_flock,
  940. .splice_read = generic_file_splice_read,
  941. .splice_write = iter_file_splice_write,
  942. .setlease = simple_nosetlease,
  943. .fallocate = gfs2_fallocate,
  944. };
  945. const struct file_operations gfs2_dir_fops = {
  946. .iterate = gfs2_readdir,
  947. .unlocked_ioctl = gfs2_ioctl,
  948. .open = gfs2_open,
  949. .release = gfs2_release,
  950. .fsync = gfs2_fsync,
  951. .lock = gfs2_lock,
  952. .flock = gfs2_flock,
  953. .llseek = default_llseek,
  954. };
  955. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  956. const struct file_operations gfs2_file_fops_nolock = {
  957. .llseek = gfs2_llseek,
  958. .read = new_sync_read,
  959. .read_iter = generic_file_read_iter,
  960. .write = new_sync_write,
  961. .write_iter = gfs2_file_write_iter,
  962. .unlocked_ioctl = gfs2_ioctl,
  963. .mmap = gfs2_mmap,
  964. .open = gfs2_open,
  965. .release = gfs2_release,
  966. .fsync = gfs2_fsync,
  967. .splice_read = generic_file_splice_read,
  968. .splice_write = iter_file_splice_write,
  969. .setlease = generic_setlease,
  970. .fallocate = gfs2_fallocate,
  971. };
  972. const struct file_operations gfs2_dir_fops_nolock = {
  973. .iterate = gfs2_readdir,
  974. .unlocked_ioctl = gfs2_ioctl,
  975. .open = gfs2_open,
  976. .release = gfs2_release,
  977. .fsync = gfs2_fsync,
  978. .llseek = default_llseek,
  979. };