file.c 25 KB

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