file.c 26 KB

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