file.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/compat.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/uio.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/mm.h>
  15. #include <linux/mount.h>
  16. #include <linux/fs.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/falloc.h>
  19. #include <linux/swap.h>
  20. #include <linux/crc32.h>
  21. #include <linux/writeback.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/dlm.h>
  24. #include <linux/dlm_plock.h>
  25. #include <linux/delay.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/fileattr.h>
  28. #include "gfs2.h"
  29. #include "incore.h"
  30. #include "bmap.h"
  31. #include "aops.h"
  32. #include "dir.h"
  33. #include "glock.h"
  34. #include "glops.h"
  35. #include "inode.h"
  36. #include "log.h"
  37. #include "meta_io.h"
  38. #include "quota.h"
  39. #include "rgrp.h"
  40. #include "trans.h"
  41. #include "util.h"
  42. /**
  43. * gfs2_llseek - seek to a location in a file
  44. * @file: the file
  45. * @offset: the offset
  46. * @whence: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
  47. *
  48. * SEEK_END requires the glock for the file because it references the
  49. * file's size.
  50. *
  51. * Returns: The new offset, or errno
  52. */
  53. static loff_t gfs2_llseek(struct file *file, loff_t offset, int whence)
  54. {
  55. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  56. struct gfs2_holder i_gh;
  57. loff_t error;
  58. switch (whence) {
  59. case SEEK_END:
  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, whence);
  64. gfs2_glock_dq_uninit(&i_gh);
  65. }
  66. break;
  67. case SEEK_DATA:
  68. error = gfs2_seek_data(file, offset);
  69. break;
  70. case SEEK_HOLE:
  71. error = gfs2_seek_hole(file, offset);
  72. break;
  73. case SEEK_CUR:
  74. case SEEK_SET:
  75. /*
  76. * These don't reference inode->i_size and don't depend on the
  77. * block mapping, so we don't need the glock.
  78. */
  79. error = generic_file_llseek(file, offset, whence);
  80. break;
  81. default:
  82. error = -EINVAL;
  83. }
  84. return error;
  85. }
  86. /**
  87. * gfs2_readdir - Iterator for a directory
  88. * @file: The directory to read from
  89. * @ctx: What to feed directory entries to
  90. *
  91. * Returns: errno
  92. */
  93. static int gfs2_readdir(struct file *file, struct dir_context *ctx)
  94. {
  95. struct inode *dir = file->f_mapping->host;
  96. struct gfs2_inode *dip = GFS2_I(dir);
  97. struct gfs2_holder d_gh;
  98. int error;
  99. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  100. if (error)
  101. return error;
  102. error = gfs2_dir_read(dir, ctx, &file->f_ra);
  103. gfs2_glock_dq_uninit(&d_gh);
  104. return error;
  105. }
  106. /*
  107. * struct fsflag_gfs2flag
  108. *
  109. * The FS_JOURNAL_DATA_FL flag maps to GFS2_DIF_INHERIT_JDATA for directories,
  110. * and to GFS2_DIF_JDATA for non-directories.
  111. */
  112. static struct {
  113. u32 fsflag;
  114. u32 gfsflag;
  115. } fsflag_gfs2flag[] = {
  116. {FS_SYNC_FL, GFS2_DIF_SYNC},
  117. {FS_IMMUTABLE_FL, GFS2_DIF_IMMUTABLE},
  118. {FS_APPEND_FL, GFS2_DIF_APPENDONLY},
  119. {FS_NOATIME_FL, GFS2_DIF_NOATIME},
  120. {FS_INDEX_FL, GFS2_DIF_EXHASH},
  121. {FS_TOPDIR_FL, GFS2_DIF_TOPDIR},
  122. {FS_JOURNAL_DATA_FL, GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA},
  123. };
  124. static inline u32 gfs2_gfsflags_to_fsflags(struct inode *inode, u32 gfsflags)
  125. {
  126. int i;
  127. u32 fsflags = 0;
  128. if (S_ISDIR(inode->i_mode))
  129. gfsflags &= ~GFS2_DIF_JDATA;
  130. else
  131. gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
  132. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
  133. if (gfsflags & fsflag_gfs2flag[i].gfsflag)
  134. fsflags |= fsflag_gfs2flag[i].fsflag;
  135. return fsflags;
  136. }
  137. int gfs2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  138. {
  139. struct inode *inode = d_inode(dentry);
  140. struct gfs2_inode *ip = GFS2_I(inode);
  141. struct gfs2_holder gh;
  142. int error;
  143. u32 fsflags;
  144. if (d_is_special(dentry))
  145. return -ENOTTY;
  146. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  147. error = gfs2_glock_nq(&gh);
  148. if (error)
  149. goto out_uninit;
  150. fsflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
  151. fileattr_fill_flags(fa, fsflags);
  152. gfs2_glock_dq(&gh);
  153. out_uninit:
  154. gfs2_holder_uninit(&gh);
  155. return error;
  156. }
  157. void gfs2_set_inode_flags(struct inode *inode)
  158. {
  159. struct gfs2_inode *ip = GFS2_I(inode);
  160. unsigned int flags = inode->i_flags;
  161. flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
  162. if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
  163. flags |= S_NOSEC;
  164. if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
  165. flags |= S_IMMUTABLE;
  166. if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
  167. flags |= S_APPEND;
  168. if (ip->i_diskflags & GFS2_DIF_NOATIME)
  169. flags |= S_NOATIME;
  170. if (ip->i_diskflags & GFS2_DIF_SYNC)
  171. flags |= S_SYNC;
  172. inode->i_flags = flags;
  173. }
  174. /* Flags that can be set by user space */
  175. #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
  176. GFS2_DIF_IMMUTABLE| \
  177. GFS2_DIF_APPENDONLY| \
  178. GFS2_DIF_NOATIME| \
  179. GFS2_DIF_SYNC| \
  180. GFS2_DIF_TOPDIR| \
  181. GFS2_DIF_INHERIT_JDATA)
  182. /**
  183. * do_gfs2_set_flags - set flags on an inode
  184. * @inode: The inode
  185. * @reqflags: The flags to set
  186. * @mask: Indicates which flags are valid
  187. *
  188. */
  189. static int do_gfs2_set_flags(struct inode *inode, u32 reqflags, u32 mask)
  190. {
  191. struct gfs2_inode *ip = GFS2_I(inode);
  192. struct gfs2_sbd *sdp = GFS2_SB(inode);
  193. struct buffer_head *bh;
  194. struct gfs2_holder gh;
  195. int error;
  196. u32 new_flags, flags;
  197. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  198. if (error)
  199. return error;
  200. error = 0;
  201. flags = ip->i_diskflags;
  202. new_flags = (flags & ~mask) | (reqflags & mask);
  203. if ((new_flags ^ flags) == 0)
  204. goto out;
  205. if (!IS_IMMUTABLE(inode)) {
  206. error = gfs2_permission(&init_user_ns, inode, MAY_WRITE);
  207. if (error)
  208. goto out;
  209. }
  210. if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
  211. if (new_flags & GFS2_DIF_JDATA)
  212. gfs2_log_flush(sdp, ip->i_gl,
  213. GFS2_LOG_HEAD_FLUSH_NORMAL |
  214. GFS2_LFC_SET_FLAGS);
  215. error = filemap_fdatawrite(inode->i_mapping);
  216. if (error)
  217. goto out;
  218. error = filemap_fdatawait(inode->i_mapping);
  219. if (error)
  220. goto out;
  221. if (new_flags & GFS2_DIF_JDATA)
  222. gfs2_ordered_del_inode(ip);
  223. }
  224. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  225. if (error)
  226. goto out;
  227. error = gfs2_meta_inode_buffer(ip, &bh);
  228. if (error)
  229. goto out_trans_end;
  230. inode->i_ctime = current_time(inode);
  231. gfs2_trans_add_meta(ip->i_gl, bh);
  232. ip->i_diskflags = new_flags;
  233. gfs2_dinode_out(ip, bh->b_data);
  234. brelse(bh);
  235. gfs2_set_inode_flags(inode);
  236. gfs2_set_aops(inode);
  237. out_trans_end:
  238. gfs2_trans_end(sdp);
  239. out:
  240. gfs2_glock_dq_uninit(&gh);
  241. return error;
  242. }
  243. int gfs2_fileattr_set(struct user_namespace *mnt_userns,
  244. struct dentry *dentry, struct fileattr *fa)
  245. {
  246. struct inode *inode = d_inode(dentry);
  247. u32 fsflags = fa->flags, gfsflags = 0;
  248. u32 mask;
  249. int i;
  250. if (d_is_special(dentry))
  251. return -ENOTTY;
  252. if (fileattr_has_fsx(fa))
  253. return -EOPNOTSUPP;
  254. for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
  255. if (fsflags & fsflag_gfs2flag[i].fsflag) {
  256. fsflags &= ~fsflag_gfs2flag[i].fsflag;
  257. gfsflags |= fsflag_gfs2flag[i].gfsflag;
  258. }
  259. }
  260. if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
  261. return -EINVAL;
  262. mask = GFS2_FLAGS_USER_SET;
  263. if (S_ISDIR(inode->i_mode)) {
  264. mask &= ~GFS2_DIF_JDATA;
  265. } else {
  266. /* The GFS2_DIF_TOPDIR flag is only valid for directories. */
  267. if (gfsflags & GFS2_DIF_TOPDIR)
  268. return -EINVAL;
  269. mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
  270. }
  271. return do_gfs2_set_flags(inode, gfsflags, mask);
  272. }
  273. static int gfs2_getlabel(struct file *filp, char __user *label)
  274. {
  275. struct inode *inode = file_inode(filp);
  276. struct gfs2_sbd *sdp = GFS2_SB(inode);
  277. if (copy_to_user(label, sdp->sd_sb.sb_locktable, GFS2_LOCKNAME_LEN))
  278. return -EFAULT;
  279. return 0;
  280. }
  281. static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  282. {
  283. switch(cmd) {
  284. case FITRIM:
  285. return gfs2_fitrim(filp, (void __user *)arg);
  286. case FS_IOC_GETFSLABEL:
  287. return gfs2_getlabel(filp, (char __user *)arg);
  288. }
  289. return -ENOTTY;
  290. }
  291. #ifdef CONFIG_COMPAT
  292. static long gfs2_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  293. {
  294. switch(cmd) {
  295. /* Keep this list in sync with gfs2_ioctl */
  296. case FITRIM:
  297. case FS_IOC_GETFSLABEL:
  298. break;
  299. default:
  300. return -ENOIOCTLCMD;
  301. }
  302. return gfs2_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  303. }
  304. #else
  305. #define gfs2_compat_ioctl NULL
  306. #endif
  307. /**
  308. * gfs2_size_hint - Give a hint to the size of a write request
  309. * @filep: The struct file
  310. * @offset: The file offset of the write
  311. * @size: The length of the write
  312. *
  313. * When we are about to do a write, this function records the total
  314. * write size in order to provide a suitable hint to the lower layers
  315. * about how many blocks will be required.
  316. *
  317. */
  318. static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
  319. {
  320. struct inode *inode = file_inode(filep);
  321. struct gfs2_sbd *sdp = GFS2_SB(inode);
  322. struct gfs2_inode *ip = GFS2_I(inode);
  323. size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  324. int hint = min_t(size_t, INT_MAX, blks);
  325. if (hint > atomic_read(&ip->i_sizehint))
  326. atomic_set(&ip->i_sizehint, hint);
  327. }
  328. /**
  329. * gfs2_allocate_page_backing - Allocate blocks for a write fault
  330. * @page: The (locked) page to allocate backing for
  331. * @length: Size of the allocation
  332. *
  333. * We try to allocate all the blocks required for the page in one go. This
  334. * might fail for various reasons, so we keep trying until all the blocks to
  335. * back this page are allocated. If some of the blocks are already allocated,
  336. * that is ok too.
  337. */
  338. static int gfs2_allocate_page_backing(struct page *page, unsigned int length)
  339. {
  340. u64 pos = page_offset(page);
  341. do {
  342. struct iomap iomap = { };
  343. if (gfs2_iomap_alloc(page->mapping->host, pos, length, &iomap))
  344. return -EIO;
  345. if (length < iomap.length)
  346. iomap.length = length;
  347. length -= iomap.length;
  348. pos += iomap.length;
  349. } while (length > 0);
  350. return 0;
  351. }
  352. /**
  353. * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
  354. * @vmf: The virtual memory fault containing the page to become writable
  355. *
  356. * When the page becomes writable, we need to ensure that we have
  357. * blocks allocated on disk to back that page.
  358. */
  359. static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
  360. {
  361. struct page *page = vmf->page;
  362. struct inode *inode = file_inode(vmf->vma->vm_file);
  363. struct gfs2_inode *ip = GFS2_I(inode);
  364. struct gfs2_sbd *sdp = GFS2_SB(inode);
  365. struct gfs2_alloc_parms ap = { .aflags = 0, };
  366. u64 offset = page_offset(page);
  367. unsigned int data_blocks, ind_blocks, rblocks;
  368. vm_fault_t ret = VM_FAULT_LOCKED;
  369. struct gfs2_holder gh;
  370. unsigned int length;
  371. loff_t size;
  372. int err;
  373. sb_start_pagefault(inode->i_sb);
  374. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  375. err = gfs2_glock_nq(&gh);
  376. if (err) {
  377. ret = block_page_mkwrite_return(err);
  378. goto out_uninit;
  379. }
  380. /* Check page index against inode size */
  381. size = i_size_read(inode);
  382. if (offset >= size) {
  383. ret = VM_FAULT_SIGBUS;
  384. goto out_unlock;
  385. }
  386. /* Update file times before taking page lock */
  387. file_update_time(vmf->vma->vm_file);
  388. /* page is wholly or partially inside EOF */
  389. if (size - offset < PAGE_SIZE)
  390. length = size - offset;
  391. else
  392. length = PAGE_SIZE;
  393. gfs2_size_hint(vmf->vma->vm_file, offset, length);
  394. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  395. set_bit(GIF_SW_PAGED, &ip->i_flags);
  396. /*
  397. * iomap_writepage / iomap_writepages currently don't support inline
  398. * files, so always unstuff here.
  399. */
  400. if (!gfs2_is_stuffed(ip) &&
  401. !gfs2_write_alloc_required(ip, offset, length)) {
  402. lock_page(page);
  403. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  404. ret = VM_FAULT_NOPAGE;
  405. unlock_page(page);
  406. }
  407. goto out_unlock;
  408. }
  409. err = gfs2_rindex_update(sdp);
  410. if (err) {
  411. ret = block_page_mkwrite_return(err);
  412. goto out_unlock;
  413. }
  414. gfs2_write_calc_reserv(ip, length, &data_blocks, &ind_blocks);
  415. ap.target = data_blocks + ind_blocks;
  416. err = gfs2_quota_lock_check(ip, &ap);
  417. if (err) {
  418. ret = block_page_mkwrite_return(err);
  419. goto out_unlock;
  420. }
  421. err = gfs2_inplace_reserve(ip, &ap);
  422. if (err) {
  423. ret = block_page_mkwrite_return(err);
  424. goto out_quota_unlock;
  425. }
  426. rblocks = RES_DINODE + ind_blocks;
  427. if (gfs2_is_jdata(ip))
  428. rblocks += data_blocks ? data_blocks : 1;
  429. if (ind_blocks || data_blocks) {
  430. rblocks += RES_STATFS + RES_QUOTA;
  431. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  432. }
  433. err = gfs2_trans_begin(sdp, rblocks, 0);
  434. if (err) {
  435. ret = block_page_mkwrite_return(err);
  436. goto out_trans_fail;
  437. }
  438. /* Unstuff, if required, and allocate backing blocks for page */
  439. if (gfs2_is_stuffed(ip)) {
  440. err = gfs2_unstuff_dinode(ip);
  441. if (err) {
  442. ret = block_page_mkwrite_return(err);
  443. goto out_trans_end;
  444. }
  445. }
  446. lock_page(page);
  447. /* If truncated, we must retry the operation, we may have raced
  448. * with the glock demotion code.
  449. */
  450. if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
  451. ret = VM_FAULT_NOPAGE;
  452. goto out_page_locked;
  453. }
  454. err = gfs2_allocate_page_backing(page, length);
  455. if (err)
  456. ret = block_page_mkwrite_return(err);
  457. out_page_locked:
  458. if (ret != VM_FAULT_LOCKED)
  459. unlock_page(page);
  460. out_trans_end:
  461. gfs2_trans_end(sdp);
  462. out_trans_fail:
  463. gfs2_inplace_release(ip);
  464. out_quota_unlock:
  465. gfs2_quota_unlock(ip);
  466. out_unlock:
  467. gfs2_glock_dq(&gh);
  468. out_uninit:
  469. gfs2_holder_uninit(&gh);
  470. if (ret == VM_FAULT_LOCKED) {
  471. set_page_dirty(page);
  472. wait_for_stable_page(page);
  473. }
  474. sb_end_pagefault(inode->i_sb);
  475. return ret;
  476. }
  477. static vm_fault_t gfs2_fault(struct vm_fault *vmf)
  478. {
  479. struct inode *inode = file_inode(vmf->vma->vm_file);
  480. struct gfs2_inode *ip = GFS2_I(inode);
  481. struct gfs2_holder gh;
  482. vm_fault_t ret;
  483. int err;
  484. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  485. err = gfs2_glock_nq(&gh);
  486. if (err) {
  487. ret = block_page_mkwrite_return(err);
  488. goto out_uninit;
  489. }
  490. ret = filemap_fault(vmf);
  491. gfs2_glock_dq(&gh);
  492. out_uninit:
  493. gfs2_holder_uninit(&gh);
  494. return ret;
  495. }
  496. static const struct vm_operations_struct gfs2_vm_ops = {
  497. .fault = gfs2_fault,
  498. .map_pages = filemap_map_pages,
  499. .page_mkwrite = gfs2_page_mkwrite,
  500. };
  501. /**
  502. * gfs2_mmap
  503. * @file: The file to map
  504. * @vma: The VMA which described the mapping
  505. *
  506. * There is no need to get a lock here unless we should be updating
  507. * atime. We ignore any locking errors since the only consequence is
  508. * a missed atime update (which will just be deferred until later).
  509. *
  510. * Returns: 0
  511. */
  512. static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
  513. {
  514. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  515. if (!(file->f_flags & O_NOATIME) &&
  516. !IS_NOATIME(&ip->i_inode)) {
  517. struct gfs2_holder i_gh;
  518. int error;
  519. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  520. &i_gh);
  521. if (error)
  522. return error;
  523. /* grab lock to update inode */
  524. gfs2_glock_dq_uninit(&i_gh);
  525. file_accessed(file);
  526. }
  527. vma->vm_ops = &gfs2_vm_ops;
  528. return 0;
  529. }
  530. /**
  531. * gfs2_open_common - This is common to open and atomic_open
  532. * @inode: The inode being opened
  533. * @file: The file being opened
  534. *
  535. * This maybe called under a glock or not depending upon how it has
  536. * been called. We must always be called under a glock for regular
  537. * files, however. For other file types, it does not matter whether
  538. * we hold the glock or not.
  539. *
  540. * Returns: Error code or 0 for success
  541. */
  542. int gfs2_open_common(struct inode *inode, struct file *file)
  543. {
  544. struct gfs2_file *fp;
  545. int ret;
  546. if (S_ISREG(inode->i_mode)) {
  547. ret = generic_file_open(inode, file);
  548. if (ret)
  549. return ret;
  550. }
  551. fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
  552. if (!fp)
  553. return -ENOMEM;
  554. mutex_init(&fp->f_fl_mutex);
  555. gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
  556. file->private_data = fp;
  557. if (file->f_mode & FMODE_WRITE) {
  558. ret = gfs2_qa_get(GFS2_I(inode));
  559. if (ret)
  560. goto fail;
  561. }
  562. return 0;
  563. fail:
  564. kfree(file->private_data);
  565. file->private_data = NULL;
  566. return ret;
  567. }
  568. /**
  569. * gfs2_open - open a file
  570. * @inode: the inode to open
  571. * @file: the struct file for this opening
  572. *
  573. * After atomic_open, this function is only used for opening files
  574. * which are already cached. We must still get the glock for regular
  575. * files to ensure that we have the file size uptodate for the large
  576. * file check which is in the common code. That is only an issue for
  577. * regular files though.
  578. *
  579. * Returns: errno
  580. */
  581. static int gfs2_open(struct inode *inode, struct file *file)
  582. {
  583. struct gfs2_inode *ip = GFS2_I(inode);
  584. struct gfs2_holder i_gh;
  585. int error;
  586. bool need_unlock = false;
  587. if (S_ISREG(ip->i_inode.i_mode)) {
  588. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
  589. &i_gh);
  590. if (error)
  591. return error;
  592. need_unlock = true;
  593. }
  594. error = gfs2_open_common(inode, file);
  595. if (need_unlock)
  596. gfs2_glock_dq_uninit(&i_gh);
  597. return error;
  598. }
  599. /**
  600. * gfs2_release - called to close a struct file
  601. * @inode: the inode the struct file belongs to
  602. * @file: the struct file being closed
  603. *
  604. * Returns: errno
  605. */
  606. static int gfs2_release(struct inode *inode, struct file *file)
  607. {
  608. struct gfs2_inode *ip = GFS2_I(inode);
  609. kfree(file->private_data);
  610. file->private_data = NULL;
  611. if (file->f_mode & FMODE_WRITE) {
  612. if (gfs2_rs_active(&ip->i_res))
  613. gfs2_rs_delete(ip);
  614. gfs2_qa_put(ip);
  615. }
  616. return 0;
  617. }
  618. /**
  619. * gfs2_fsync - sync the dirty data for a file (across the cluster)
  620. * @file: the file that points to the dentry
  621. * @start: the start position in the file to sync
  622. * @end: the end position in the file to sync
  623. * @datasync: set if we can ignore timestamp changes
  624. *
  625. * We split the data flushing here so that we don't wait for the data
  626. * until after we've also sent the metadata to disk. Note that for
  627. * data=ordered, we will write & wait for the data at the log flush
  628. * stage anyway, so this is unlikely to make much of a difference
  629. * except in the data=writeback case.
  630. *
  631. * If the fdatawrite fails due to any reason except -EIO, we will
  632. * continue the remainder of the fsync, although we'll still report
  633. * the error at the end. This is to match filemap_write_and_wait_range()
  634. * behaviour.
  635. *
  636. * Returns: errno
  637. */
  638. static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
  639. int datasync)
  640. {
  641. struct address_space *mapping = file->f_mapping;
  642. struct inode *inode = mapping->host;
  643. int sync_state = inode->i_state & I_DIRTY;
  644. struct gfs2_inode *ip = GFS2_I(inode);
  645. int ret = 0, ret1 = 0;
  646. if (mapping->nrpages) {
  647. ret1 = filemap_fdatawrite_range(mapping, start, end);
  648. if (ret1 == -EIO)
  649. return ret1;
  650. }
  651. if (!gfs2_is_jdata(ip))
  652. sync_state &= ~I_DIRTY_PAGES;
  653. if (datasync)
  654. sync_state &= ~I_DIRTY_SYNC;
  655. if (sync_state) {
  656. ret = sync_inode_metadata(inode, 1);
  657. if (ret)
  658. return ret;
  659. if (gfs2_is_jdata(ip))
  660. ret = file_write_and_wait(file);
  661. if (ret)
  662. return ret;
  663. gfs2_ail_flush(ip->i_gl, 1);
  664. }
  665. if (mapping->nrpages)
  666. ret = file_fdatawait_range(file, start, end);
  667. return ret ? ret : ret1;
  668. }
  669. static inline bool should_fault_in_pages(struct iov_iter *i,
  670. struct kiocb *iocb,
  671. size_t *prev_count,
  672. size_t *window_size)
  673. {
  674. size_t count = iov_iter_count(i);
  675. size_t size, offs;
  676. if (!count)
  677. return false;
  678. if (!user_backed_iter(i))
  679. return false;
  680. /*
  681. * Try to fault in multiple pages initially. When that doesn't result
  682. * in any progress, fall back to a single page.
  683. */
  684. size = PAGE_SIZE;
  685. offs = offset_in_page(iocb->ki_pos);
  686. if (*prev_count != count) {
  687. size_t nr_dirtied;
  688. nr_dirtied = max(current->nr_dirtied_pause -
  689. current->nr_dirtied, 8);
  690. size = min_t(size_t, SZ_1M, nr_dirtied << PAGE_SHIFT);
  691. }
  692. *prev_count = count;
  693. *window_size = size - offs;
  694. return true;
  695. }
  696. static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
  697. struct gfs2_holder *gh)
  698. {
  699. struct file *file = iocb->ki_filp;
  700. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  701. size_t prev_count = 0, window_size = 0;
  702. size_t read = 0;
  703. ssize_t ret;
  704. /*
  705. * In this function, we disable page faults when we're holding the
  706. * inode glock while doing I/O. If a page fault occurs, we indicate
  707. * that the inode glock may be dropped, fault in the pages manually,
  708. * and retry.
  709. *
  710. * Unlike generic_file_read_iter, for reads, iomap_dio_rw can trigger
  711. * physical as well as manual page faults, and we need to disable both
  712. * kinds.
  713. *
  714. * For direct I/O, gfs2 takes the inode glock in deferred mode. This
  715. * locking mode is compatible with other deferred holders, so multiple
  716. * processes and nodes can do direct I/O to a file at the same time.
  717. * There's no guarantee that reads or writes will be atomic. Any
  718. * coordination among readers and writers needs to happen externally.
  719. */
  720. if (!iov_iter_count(to))
  721. return 0; /* skip atime */
  722. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
  723. retry:
  724. ret = gfs2_glock_nq(gh);
  725. if (ret)
  726. goto out_uninit;
  727. pagefault_disable();
  728. to->nofault = true;
  729. ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL,
  730. IOMAP_DIO_PARTIAL, NULL, read);
  731. to->nofault = false;
  732. pagefault_enable();
  733. if (ret <= 0 && ret != -EFAULT)
  734. goto out_unlock;
  735. /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
  736. if (ret > 0)
  737. read = ret;
  738. if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
  739. gfs2_glock_dq(gh);
  740. window_size -= fault_in_iov_iter_writeable(to, window_size);
  741. if (window_size)
  742. goto retry;
  743. }
  744. out_unlock:
  745. if (gfs2_holder_queued(gh))
  746. gfs2_glock_dq(gh);
  747. out_uninit:
  748. gfs2_holder_uninit(gh);
  749. /* User space doesn't expect partial success. */
  750. if (ret < 0)
  751. return ret;
  752. return read;
  753. }
  754. static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
  755. struct gfs2_holder *gh)
  756. {
  757. struct file *file = iocb->ki_filp;
  758. struct inode *inode = file->f_mapping->host;
  759. struct gfs2_inode *ip = GFS2_I(inode);
  760. size_t prev_count = 0, window_size = 0;
  761. size_t written = 0;
  762. bool enough_retries;
  763. ssize_t ret;
  764. /*
  765. * In this function, we disable page faults when we're holding the
  766. * inode glock while doing I/O. If a page fault occurs, we indicate
  767. * that the inode glock may be dropped, fault in the pages manually,
  768. * and retry.
  769. *
  770. * For writes, iomap_dio_rw only triggers manual page faults, so we
  771. * don't need to disable physical ones.
  772. */
  773. /*
  774. * Deferred lock, even if its a write, since we do no allocation on
  775. * this path. All we need to change is the atime, and this lock mode
  776. * ensures that other nodes have flushed their buffered read caches
  777. * (i.e. their page cache entries for this inode). We do not,
  778. * unfortunately, have the option of only flushing a range like the
  779. * VFS does.
  780. */
  781. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
  782. retry:
  783. ret = gfs2_glock_nq(gh);
  784. if (ret)
  785. goto out_uninit;
  786. /* Silently fall back to buffered I/O when writing beyond EOF */
  787. if (iocb->ki_pos + iov_iter_count(from) > i_size_read(&ip->i_inode))
  788. goto out_unlock;
  789. from->nofault = true;
  790. ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
  791. IOMAP_DIO_PARTIAL, NULL, written);
  792. from->nofault = false;
  793. if (ret <= 0) {
  794. if (ret == -ENOTBLK)
  795. ret = 0;
  796. if (ret != -EFAULT)
  797. goto out_unlock;
  798. }
  799. /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
  800. if (ret > 0)
  801. written = ret;
  802. enough_retries = prev_count == iov_iter_count(from) &&
  803. window_size <= PAGE_SIZE;
  804. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  805. gfs2_glock_dq(gh);
  806. window_size -= fault_in_iov_iter_readable(from, window_size);
  807. if (window_size) {
  808. if (!enough_retries)
  809. goto retry;
  810. /* fall back to buffered I/O */
  811. ret = 0;
  812. }
  813. }
  814. out_unlock:
  815. if (gfs2_holder_queued(gh))
  816. gfs2_glock_dq(gh);
  817. out_uninit:
  818. gfs2_holder_uninit(gh);
  819. /* User space doesn't expect partial success. */
  820. if (ret < 0)
  821. return ret;
  822. return written;
  823. }
  824. static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  825. {
  826. struct gfs2_inode *ip;
  827. struct gfs2_holder gh;
  828. size_t prev_count = 0, window_size = 0;
  829. size_t read = 0;
  830. ssize_t ret;
  831. /*
  832. * In this function, we disable page faults when we're holding the
  833. * inode glock while doing I/O. If a page fault occurs, we indicate
  834. * that the inode glock may be dropped, fault in the pages manually,
  835. * and retry.
  836. */
  837. if (iocb->ki_flags & IOCB_DIRECT)
  838. return gfs2_file_direct_read(iocb, to, &gh);
  839. pagefault_disable();
  840. iocb->ki_flags |= IOCB_NOIO;
  841. ret = generic_file_read_iter(iocb, to);
  842. iocb->ki_flags &= ~IOCB_NOIO;
  843. pagefault_enable();
  844. if (ret >= 0) {
  845. if (!iov_iter_count(to))
  846. return ret;
  847. read = ret;
  848. } else if (ret != -EFAULT) {
  849. if (ret != -EAGAIN)
  850. return ret;
  851. if (iocb->ki_flags & IOCB_NOWAIT)
  852. return ret;
  853. }
  854. ip = GFS2_I(iocb->ki_filp->f_mapping->host);
  855. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  856. retry:
  857. ret = gfs2_glock_nq(&gh);
  858. if (ret)
  859. goto out_uninit;
  860. pagefault_disable();
  861. ret = generic_file_read_iter(iocb, to);
  862. pagefault_enable();
  863. if (ret <= 0 && ret != -EFAULT)
  864. goto out_unlock;
  865. if (ret > 0)
  866. read += ret;
  867. if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
  868. gfs2_glock_dq(&gh);
  869. window_size -= fault_in_iov_iter_writeable(to, window_size);
  870. if (window_size)
  871. goto retry;
  872. }
  873. out_unlock:
  874. if (gfs2_holder_queued(&gh))
  875. gfs2_glock_dq(&gh);
  876. out_uninit:
  877. gfs2_holder_uninit(&gh);
  878. return read ? read : ret;
  879. }
  880. static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
  881. struct iov_iter *from,
  882. struct gfs2_holder *gh)
  883. {
  884. struct file *file = iocb->ki_filp;
  885. struct inode *inode = file_inode(file);
  886. struct gfs2_inode *ip = GFS2_I(inode);
  887. struct gfs2_sbd *sdp = GFS2_SB(inode);
  888. struct gfs2_holder *statfs_gh = NULL;
  889. size_t prev_count = 0, window_size = 0;
  890. size_t orig_count = iov_iter_count(from);
  891. size_t written = 0;
  892. ssize_t ret;
  893. /*
  894. * In this function, we disable page faults when we're holding the
  895. * inode glock while doing I/O. If a page fault occurs, we indicate
  896. * that the inode glock may be dropped, fault in the pages manually,
  897. * and retry.
  898. */
  899. if (inode == sdp->sd_rindex) {
  900. statfs_gh = kmalloc(sizeof(*statfs_gh), GFP_NOFS);
  901. if (!statfs_gh)
  902. return -ENOMEM;
  903. }
  904. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, gh);
  905. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  906. retry:
  907. window_size -= fault_in_iov_iter_readable(from, window_size);
  908. if (!window_size) {
  909. ret = -EFAULT;
  910. goto out_uninit;
  911. }
  912. from->count = min(from->count, window_size);
  913. }
  914. ret = gfs2_glock_nq(gh);
  915. if (ret)
  916. goto out_uninit;
  917. if (inode == sdp->sd_rindex) {
  918. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  919. ret = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
  920. GL_NOCACHE, statfs_gh);
  921. if (ret)
  922. goto out_unlock;
  923. }
  924. current->backing_dev_info = inode_to_bdi(inode);
  925. pagefault_disable();
  926. ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
  927. pagefault_enable();
  928. current->backing_dev_info = NULL;
  929. if (ret > 0) {
  930. iocb->ki_pos += ret;
  931. written += ret;
  932. }
  933. if (inode == sdp->sd_rindex)
  934. gfs2_glock_dq_uninit(statfs_gh);
  935. if (ret <= 0 && ret != -EFAULT)
  936. goto out_unlock;
  937. from->count = orig_count - written;
  938. if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
  939. gfs2_glock_dq(gh);
  940. goto retry;
  941. }
  942. out_unlock:
  943. if (gfs2_holder_queued(gh))
  944. gfs2_glock_dq(gh);
  945. out_uninit:
  946. gfs2_holder_uninit(gh);
  947. kfree(statfs_gh);
  948. from->count = orig_count - written;
  949. return written ? written : ret;
  950. }
  951. /**
  952. * gfs2_file_write_iter - Perform a write to a file
  953. * @iocb: The io context
  954. * @from: The data to write
  955. *
  956. * We have to do a lock/unlock here to refresh the inode size for
  957. * O_APPEND writes, otherwise we can land up writing at the wrong
  958. * offset. There is still a race, but provided the app is using its
  959. * own file locking, this will make O_APPEND work as expected.
  960. *
  961. */
  962. static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  963. {
  964. struct file *file = iocb->ki_filp;
  965. struct inode *inode = file_inode(file);
  966. struct gfs2_inode *ip = GFS2_I(inode);
  967. struct gfs2_holder gh;
  968. ssize_t ret;
  969. gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
  970. if (iocb->ki_flags & IOCB_APPEND) {
  971. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  972. if (ret)
  973. return ret;
  974. gfs2_glock_dq_uninit(&gh);
  975. }
  976. inode_lock(inode);
  977. ret = generic_write_checks(iocb, from);
  978. if (ret <= 0)
  979. goto out_unlock;
  980. ret = file_remove_privs(file);
  981. if (ret)
  982. goto out_unlock;
  983. ret = file_update_time(file);
  984. if (ret)
  985. goto out_unlock;
  986. if (iocb->ki_flags & IOCB_DIRECT) {
  987. struct address_space *mapping = file->f_mapping;
  988. ssize_t buffered, ret2;
  989. ret = gfs2_file_direct_write(iocb, from, &gh);
  990. if (ret < 0 || !iov_iter_count(from))
  991. goto out_unlock;
  992. iocb->ki_flags |= IOCB_DSYNC;
  993. buffered = gfs2_file_buffered_write(iocb, from, &gh);
  994. if (unlikely(buffered <= 0)) {
  995. if (!ret)
  996. ret = buffered;
  997. goto out_unlock;
  998. }
  999. /*
  1000. * We need to ensure that the page cache pages are written to
  1001. * disk and invalidated to preserve the expected O_DIRECT
  1002. * semantics. If the writeback or invalidate fails, only report
  1003. * the direct I/O range as we don't know if the buffered pages
  1004. * made it to disk.
  1005. */
  1006. ret2 = generic_write_sync(iocb, buffered);
  1007. invalidate_mapping_pages(mapping,
  1008. (iocb->ki_pos - buffered) >> PAGE_SHIFT,
  1009. (iocb->ki_pos - 1) >> PAGE_SHIFT);
  1010. if (!ret || ret2 > 0)
  1011. ret += ret2;
  1012. } else {
  1013. ret = gfs2_file_buffered_write(iocb, from, &gh);
  1014. if (likely(ret > 0))
  1015. ret = generic_write_sync(iocb, ret);
  1016. }
  1017. out_unlock:
  1018. inode_unlock(inode);
  1019. return ret;
  1020. }
  1021. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1022. int mode)
  1023. {
  1024. struct super_block *sb = inode->i_sb;
  1025. struct gfs2_inode *ip = GFS2_I(inode);
  1026. loff_t end = offset + len;
  1027. struct buffer_head *dibh;
  1028. int error;
  1029. error = gfs2_meta_inode_buffer(ip, &dibh);
  1030. if (unlikely(error))
  1031. return error;
  1032. gfs2_trans_add_meta(ip->i_gl, dibh);
  1033. if (gfs2_is_stuffed(ip)) {
  1034. error = gfs2_unstuff_dinode(ip);
  1035. if (unlikely(error))
  1036. goto out;
  1037. }
  1038. while (offset < end) {
  1039. struct iomap iomap = { };
  1040. error = gfs2_iomap_alloc(inode, offset, end - offset, &iomap);
  1041. if (error)
  1042. goto out;
  1043. offset = iomap.offset + iomap.length;
  1044. if (!(iomap.flags & IOMAP_F_NEW))
  1045. continue;
  1046. error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
  1047. iomap.length >> inode->i_blkbits,
  1048. GFP_NOFS);
  1049. if (error) {
  1050. fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
  1051. goto out;
  1052. }
  1053. }
  1054. out:
  1055. brelse(dibh);
  1056. return error;
  1057. }
  1058. /**
  1059. * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
  1060. * blocks, determine how many bytes can be written.
  1061. * @ip: The inode in question.
  1062. * @len: Max cap of bytes. What we return in *len must be <= this.
  1063. * @data_blocks: Compute and return the number of data blocks needed
  1064. * @ind_blocks: Compute and return the number of indirect blocks needed
  1065. * @max_blocks: The total blocks available to work with.
  1066. *
  1067. * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
  1068. */
  1069. static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
  1070. unsigned int *data_blocks, unsigned int *ind_blocks,
  1071. unsigned int max_blocks)
  1072. {
  1073. loff_t max = *len;
  1074. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1075. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1076. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1077. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1078. max_data -= tmp;
  1079. }
  1080. *data_blocks = max_data;
  1081. *ind_blocks = max_blocks - max_data;
  1082. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1083. if (*len > max) {
  1084. *len = max;
  1085. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1086. }
  1087. }
  1088. static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  1089. {
  1090. struct inode *inode = file_inode(file);
  1091. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1092. struct gfs2_inode *ip = GFS2_I(inode);
  1093. struct gfs2_alloc_parms ap = { .aflags = 0, };
  1094. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1095. loff_t bytes, max_bytes, max_blks;
  1096. int error;
  1097. const loff_t pos = offset;
  1098. const loff_t count = len;
  1099. loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
  1100. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1101. loff_t max_chunk_size = UINT_MAX & bsize_mask;
  1102. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1103. offset &= bsize_mask;
  1104. len = next - offset;
  1105. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1106. if (!bytes)
  1107. bytes = UINT_MAX;
  1108. bytes &= bsize_mask;
  1109. if (bytes == 0)
  1110. bytes = sdp->sd_sb.sb_bsize;
  1111. gfs2_size_hint(file, offset, len);
  1112. gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
  1113. ap.min_target = data_blocks + ind_blocks;
  1114. while (len > 0) {
  1115. if (len < bytes)
  1116. bytes = len;
  1117. if (!gfs2_write_alloc_required(ip, offset, bytes)) {
  1118. len -= bytes;
  1119. offset += bytes;
  1120. continue;
  1121. }
  1122. /* We need to determine how many bytes we can actually
  1123. * fallocate without exceeding quota or going over the
  1124. * end of the fs. We start off optimistically by assuming
  1125. * we can write max_bytes */
  1126. max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
  1127. /* Since max_bytes is most likely a theoretical max, we
  1128. * calculate a more realistic 'bytes' to serve as a good
  1129. * starting point for the number of bytes we may be able
  1130. * to write */
  1131. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1132. ap.target = data_blocks + ind_blocks;
  1133. error = gfs2_quota_lock_check(ip, &ap);
  1134. if (error)
  1135. return error;
  1136. /* ap.allowed tells us how many blocks quota will allow
  1137. * us to write. Check if this reduces max_blks */
  1138. max_blks = UINT_MAX;
  1139. if (ap.allowed)
  1140. max_blks = ap.allowed;
  1141. error = gfs2_inplace_reserve(ip, &ap);
  1142. if (error)
  1143. goto out_qunlock;
  1144. /* check if the selected rgrp limits our max_blks further */
  1145. if (ip->i_res.rs_reserved < max_blks)
  1146. max_blks = ip->i_res.rs_reserved;
  1147. /* Almost done. Calculate bytes that can be written using
  1148. * max_blks. We also recompute max_bytes, data_blocks and
  1149. * ind_blocks */
  1150. calc_max_reserv(ip, &max_bytes, &data_blocks,
  1151. &ind_blocks, max_blks);
  1152. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1153. RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  1154. if (gfs2_is_jdata(ip))
  1155. rblocks += data_blocks ? data_blocks : 1;
  1156. error = gfs2_trans_begin(sdp, rblocks,
  1157. PAGE_SIZE >> inode->i_blkbits);
  1158. if (error)
  1159. goto out_trans_fail;
  1160. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1161. gfs2_trans_end(sdp);
  1162. if (error)
  1163. goto out_trans_fail;
  1164. len -= max_bytes;
  1165. offset += max_bytes;
  1166. gfs2_inplace_release(ip);
  1167. gfs2_quota_unlock(ip);
  1168. }
  1169. if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size)
  1170. i_size_write(inode, pos + count);
  1171. file_update_time(file);
  1172. mark_inode_dirty(inode);
  1173. if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
  1174. return vfs_fsync_range(file, pos, pos + count - 1,
  1175. (file->f_flags & __O_SYNC) ? 0 : 1);
  1176. return 0;
  1177. out_trans_fail:
  1178. gfs2_inplace_release(ip);
  1179. out_qunlock:
  1180. gfs2_quota_unlock(ip);
  1181. return error;
  1182. }
  1183. static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  1184. {
  1185. struct inode *inode = file_inode(file);
  1186. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1187. struct gfs2_inode *ip = GFS2_I(inode);
  1188. struct gfs2_holder gh;
  1189. int ret;
  1190. if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
  1191. return -EOPNOTSUPP;
  1192. /* fallocate is needed by gfs2_grow to reserve space in the rindex */
  1193. if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
  1194. return -EOPNOTSUPP;
  1195. inode_lock(inode);
  1196. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1197. ret = gfs2_glock_nq(&gh);
  1198. if (ret)
  1199. goto out_uninit;
  1200. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  1201. (offset + len) > inode->i_size) {
  1202. ret = inode_newsize_ok(inode, offset + len);
  1203. if (ret)
  1204. goto out_unlock;
  1205. }
  1206. ret = get_write_access(inode);
  1207. if (ret)
  1208. goto out_unlock;
  1209. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1210. ret = __gfs2_punch_hole(file, offset, len);
  1211. } else {
  1212. ret = __gfs2_fallocate(file, mode, offset, len);
  1213. if (ret)
  1214. gfs2_rs_deltree(&ip->i_res);
  1215. }
  1216. put_write_access(inode);
  1217. out_unlock:
  1218. gfs2_glock_dq(&gh);
  1219. out_uninit:
  1220. gfs2_holder_uninit(&gh);
  1221. inode_unlock(inode);
  1222. return ret;
  1223. }
  1224. static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
  1225. struct file *out, loff_t *ppos,
  1226. size_t len, unsigned int flags)
  1227. {
  1228. ssize_t ret;
  1229. gfs2_size_hint(out, *ppos, len);
  1230. ret = iter_file_splice_write(pipe, out, ppos, len, flags);
  1231. return ret;
  1232. }
  1233. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  1234. /**
  1235. * gfs2_lock - acquire/release a posix lock on a file
  1236. * @file: the file pointer
  1237. * @cmd: either modify or retrieve lock state, possibly wait
  1238. * @fl: type and range of lock
  1239. *
  1240. * Returns: errno
  1241. */
  1242. static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  1243. {
  1244. struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
  1245. struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
  1246. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1247. if (!(fl->fl_flags & FL_POSIX))
  1248. return -ENOLCK;
  1249. if (cmd == F_CANCELLK) {
  1250. /* Hack: */
  1251. cmd = F_SETLK;
  1252. fl->fl_type = F_UNLCK;
  1253. }
  1254. if (unlikely(gfs2_withdrawn(sdp))) {
  1255. if (fl->fl_type == F_UNLCK)
  1256. locks_lock_file_wait(file, fl);
  1257. return -EIO;
  1258. }
  1259. if (IS_GETLK(cmd))
  1260. return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
  1261. else if (fl->fl_type == F_UNLCK)
  1262. return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
  1263. else
  1264. return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
  1265. }
  1266. static void __flock_holder_uninit(struct file *file, struct gfs2_holder *fl_gh)
  1267. {
  1268. struct gfs2_glock *gl = fl_gh->gh_gl;
  1269. /*
  1270. * Make sure gfs2_glock_put() won't sleep under the file->f_lock
  1271. * spinlock.
  1272. */
  1273. gfs2_glock_hold(gl);
  1274. spin_lock(&file->f_lock);
  1275. gfs2_holder_uninit(fl_gh);
  1276. spin_unlock(&file->f_lock);
  1277. gfs2_glock_put(gl);
  1278. }
  1279. static int do_flock(struct file *file, int cmd, struct file_lock *fl)
  1280. {
  1281. struct gfs2_file *fp = file->private_data;
  1282. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1283. struct gfs2_inode *ip = GFS2_I(file_inode(file));
  1284. struct gfs2_glock *gl;
  1285. unsigned int state;
  1286. u16 flags;
  1287. int error = 0;
  1288. int sleeptime;
  1289. state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
  1290. flags = GL_EXACT | GL_NOPID;
  1291. if (!IS_SETLKW(cmd))
  1292. flags |= LM_FLAG_TRY_1CB;
  1293. mutex_lock(&fp->f_fl_mutex);
  1294. if (gfs2_holder_initialized(fl_gh)) {
  1295. struct file_lock request;
  1296. if (fl_gh->gh_state == state)
  1297. goto out;
  1298. locks_init_lock(&request);
  1299. request.fl_type = F_UNLCK;
  1300. request.fl_flags = FL_FLOCK;
  1301. locks_lock_file_wait(file, &request);
  1302. gfs2_glock_dq(fl_gh);
  1303. gfs2_holder_reinit(state, flags, fl_gh);
  1304. } else {
  1305. error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
  1306. &gfs2_flock_glops, CREATE, &gl);
  1307. if (error)
  1308. goto out;
  1309. spin_lock(&file->f_lock);
  1310. gfs2_holder_init(gl, state, flags, fl_gh);
  1311. spin_unlock(&file->f_lock);
  1312. gfs2_glock_put(gl);
  1313. }
  1314. for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
  1315. error = gfs2_glock_nq(fl_gh);
  1316. if (error != GLR_TRYFAILED)
  1317. break;
  1318. fl_gh->gh_flags &= ~LM_FLAG_TRY_1CB;
  1319. fl_gh->gh_flags |= LM_FLAG_TRY;
  1320. msleep(sleeptime);
  1321. }
  1322. if (error) {
  1323. __flock_holder_uninit(file, fl_gh);
  1324. if (error == GLR_TRYFAILED)
  1325. error = -EAGAIN;
  1326. } else {
  1327. error = locks_lock_file_wait(file, fl);
  1328. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1329. }
  1330. out:
  1331. mutex_unlock(&fp->f_fl_mutex);
  1332. return error;
  1333. }
  1334. static void do_unflock(struct file *file, struct file_lock *fl)
  1335. {
  1336. struct gfs2_file *fp = file->private_data;
  1337. struct gfs2_holder *fl_gh = &fp->f_fl_gh;
  1338. mutex_lock(&fp->f_fl_mutex);
  1339. locks_lock_file_wait(file, fl);
  1340. if (gfs2_holder_initialized(fl_gh)) {
  1341. gfs2_glock_dq(fl_gh);
  1342. __flock_holder_uninit(file, fl_gh);
  1343. }
  1344. mutex_unlock(&fp->f_fl_mutex);
  1345. }
  1346. /**
  1347. * gfs2_flock - acquire/release a flock lock on a file
  1348. * @file: the file pointer
  1349. * @cmd: either modify or retrieve lock state, possibly wait
  1350. * @fl: type and range of lock
  1351. *
  1352. * Returns: errno
  1353. */
  1354. static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  1355. {
  1356. if (!(fl->fl_flags & FL_FLOCK))
  1357. return -ENOLCK;
  1358. if (fl->fl_type == F_UNLCK) {
  1359. do_unflock(file, fl);
  1360. return 0;
  1361. } else {
  1362. return do_flock(file, cmd, fl);
  1363. }
  1364. }
  1365. const struct file_operations gfs2_file_fops = {
  1366. .llseek = gfs2_llseek,
  1367. .read_iter = gfs2_file_read_iter,
  1368. .write_iter = gfs2_file_write_iter,
  1369. .iopoll = iocb_bio_iopoll,
  1370. .unlocked_ioctl = gfs2_ioctl,
  1371. .compat_ioctl = gfs2_compat_ioctl,
  1372. .mmap = gfs2_mmap,
  1373. .open = gfs2_open,
  1374. .release = gfs2_release,
  1375. .fsync = gfs2_fsync,
  1376. .lock = gfs2_lock,
  1377. .flock = gfs2_flock,
  1378. .splice_read = generic_file_splice_read,
  1379. .splice_write = gfs2_file_splice_write,
  1380. .setlease = simple_nosetlease,
  1381. .fallocate = gfs2_fallocate,
  1382. };
  1383. const struct file_operations gfs2_dir_fops = {
  1384. .iterate_shared = gfs2_readdir,
  1385. .unlocked_ioctl = gfs2_ioctl,
  1386. .compat_ioctl = gfs2_compat_ioctl,
  1387. .open = gfs2_open,
  1388. .release = gfs2_release,
  1389. .fsync = gfs2_fsync,
  1390. .lock = gfs2_lock,
  1391. .flock = gfs2_flock,
  1392. .llseek = default_llseek,
  1393. };
  1394. #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
  1395. const struct file_operations gfs2_file_fops_nolock = {
  1396. .llseek = gfs2_llseek,
  1397. .read_iter = gfs2_file_read_iter,
  1398. .write_iter = gfs2_file_write_iter,
  1399. .iopoll = iocb_bio_iopoll,
  1400. .unlocked_ioctl = gfs2_ioctl,
  1401. .compat_ioctl = gfs2_compat_ioctl,
  1402. .mmap = gfs2_mmap,
  1403. .open = gfs2_open,
  1404. .release = gfs2_release,
  1405. .fsync = gfs2_fsync,
  1406. .splice_read = generic_file_splice_read,
  1407. .splice_write = gfs2_file_splice_write,
  1408. .setlease = generic_setlease,
  1409. .fallocate = gfs2_fallocate,
  1410. };
  1411. const struct file_operations gfs2_dir_fops_nolock = {
  1412. .iterate_shared = gfs2_readdir,
  1413. .unlocked_ioctl = gfs2_ioctl,
  1414. .compat_ioctl = gfs2_compat_ioctl,
  1415. .open = gfs2_open,
  1416. .release = gfs2_release,
  1417. .fsync = gfs2_fsync,
  1418. .llseek = default_llseek,
  1419. };