file.c 26 KB

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