file.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #include <linux/falloc.h>
  37. #include <linux/quotaops.h>
  38. #include <linux/blkdev.h>
  39. #include <cluster/masklog.h>
  40. #include "ocfs2.h"
  41. #include "alloc.h"
  42. #include "aops.h"
  43. #include "dir.h"
  44. #include "dlmglue.h"
  45. #include "extent_map.h"
  46. #include "file.h"
  47. #include "sysfile.h"
  48. #include "inode.h"
  49. #include "ioctl.h"
  50. #include "journal.h"
  51. #include "locks.h"
  52. #include "mmap.h"
  53. #include "suballoc.h"
  54. #include "super.h"
  55. #include "xattr.h"
  56. #include "acl.h"
  57. #include "quota.h"
  58. #include "refcounttree.h"
  59. #include "ocfs2_trace.h"
  60. #include "buffer_head_io.h"
  61. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  62. {
  63. struct ocfs2_file_private *fp;
  64. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  65. if (!fp)
  66. return -ENOMEM;
  67. fp->fp_file = file;
  68. mutex_init(&fp->fp_mutex);
  69. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  70. file->private_data = fp;
  71. return 0;
  72. }
  73. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  74. {
  75. struct ocfs2_file_private *fp = file->private_data;
  76. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  77. if (fp) {
  78. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  79. ocfs2_lock_res_free(&fp->fp_flock);
  80. kfree(fp);
  81. file->private_data = NULL;
  82. }
  83. }
  84. static int ocfs2_file_open(struct inode *inode, struct file *file)
  85. {
  86. int status;
  87. int mode = file->f_flags;
  88. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  89. trace_ocfs2_file_open(inode, file, file->f_path.dentry,
  90. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  91. file->f_path.dentry->d_name.len,
  92. file->f_path.dentry->d_name.name, mode);
  93. if (file->f_mode & FMODE_WRITE)
  94. dquot_initialize(inode);
  95. spin_lock(&oi->ip_lock);
  96. /* Check that the inode hasn't been wiped from disk by another
  97. * node. If it hasn't then we're safe as long as we hold the
  98. * spin lock until our increment of open count. */
  99. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  100. spin_unlock(&oi->ip_lock);
  101. status = -ENOENT;
  102. goto leave;
  103. }
  104. if (mode & O_DIRECT)
  105. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  106. oi->ip_open_count++;
  107. spin_unlock(&oi->ip_lock);
  108. status = ocfs2_init_file_private(inode, file);
  109. if (status) {
  110. /*
  111. * We want to set open count back if we're failing the
  112. * open.
  113. */
  114. spin_lock(&oi->ip_lock);
  115. oi->ip_open_count--;
  116. spin_unlock(&oi->ip_lock);
  117. }
  118. leave:
  119. return status;
  120. }
  121. static int ocfs2_file_release(struct inode *inode, struct file *file)
  122. {
  123. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  124. spin_lock(&oi->ip_lock);
  125. if (!--oi->ip_open_count)
  126. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  127. trace_ocfs2_file_release(inode, file, file->f_path.dentry,
  128. oi->ip_blkno,
  129. file->f_path.dentry->d_name.len,
  130. file->f_path.dentry->d_name.name,
  131. oi->ip_open_count);
  132. spin_unlock(&oi->ip_lock);
  133. ocfs2_free_file_private(inode, file);
  134. return 0;
  135. }
  136. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  137. {
  138. return ocfs2_init_file_private(inode, file);
  139. }
  140. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  141. {
  142. ocfs2_free_file_private(inode, file);
  143. return 0;
  144. }
  145. static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
  146. int datasync)
  147. {
  148. int err = 0;
  149. struct inode *inode = file->f_mapping->host;
  150. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  151. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  152. journal_t *journal = osb->journal->j_journal;
  153. int ret;
  154. tid_t commit_tid;
  155. bool needs_barrier = false;
  156. trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
  157. OCFS2_I(inode)->ip_blkno,
  158. file->f_path.dentry->d_name.len,
  159. file->f_path.dentry->d_name.name,
  160. (unsigned long long)datasync);
  161. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  162. return -EROFS;
  163. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  164. if (err)
  165. return err;
  166. commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid;
  167. if (journal->j_flags & JBD2_BARRIER &&
  168. !jbd2_trans_will_send_data_barrier(journal, commit_tid))
  169. needs_barrier = true;
  170. err = jbd2_complete_transaction(journal, commit_tid);
  171. if (needs_barrier) {
  172. ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  173. if (!err)
  174. err = ret;
  175. }
  176. if (err)
  177. mlog_errno(err);
  178. return (err < 0) ? -EIO : 0;
  179. }
  180. int ocfs2_should_update_atime(struct inode *inode,
  181. struct vfsmount *vfsmnt)
  182. {
  183. struct timespec now;
  184. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  185. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  186. return 0;
  187. if ((inode->i_flags & S_NOATIME) ||
  188. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  189. return 0;
  190. /*
  191. * We can be called with no vfsmnt structure - NFSD will
  192. * sometimes do this.
  193. *
  194. * Note that our action here is different than touch_atime() -
  195. * if we can't tell whether this is a noatime mount, then we
  196. * don't know whether to trust the value of s_atime_quantum.
  197. */
  198. if (vfsmnt == NULL)
  199. return 0;
  200. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  201. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  202. return 0;
  203. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  204. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  205. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  206. return 1;
  207. return 0;
  208. }
  209. now = CURRENT_TIME;
  210. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  211. return 0;
  212. else
  213. return 1;
  214. }
  215. int ocfs2_update_inode_atime(struct inode *inode,
  216. struct buffer_head *bh)
  217. {
  218. int ret;
  219. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  220. handle_t *handle;
  221. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  222. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  223. if (IS_ERR(handle)) {
  224. ret = PTR_ERR(handle);
  225. mlog_errno(ret);
  226. goto out;
  227. }
  228. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  229. OCFS2_JOURNAL_ACCESS_WRITE);
  230. if (ret) {
  231. mlog_errno(ret);
  232. goto out_commit;
  233. }
  234. /*
  235. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  236. * have i_mutex to guard against concurrent changes to other
  237. * inode fields.
  238. */
  239. inode->i_atime = CURRENT_TIME;
  240. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  241. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  242. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  243. ocfs2_journal_dirty(handle, bh);
  244. out_commit:
  245. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  246. out:
  247. return ret;
  248. }
  249. int ocfs2_set_inode_size(handle_t *handle,
  250. struct inode *inode,
  251. struct buffer_head *fe_bh,
  252. u64 new_i_size)
  253. {
  254. int status;
  255. i_size_write(inode, new_i_size);
  256. inode->i_blocks = ocfs2_inode_sector_count(inode);
  257. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  258. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  259. if (status < 0) {
  260. mlog_errno(status);
  261. goto bail;
  262. }
  263. bail:
  264. return status;
  265. }
  266. int ocfs2_simple_size_update(struct inode *inode,
  267. struct buffer_head *di_bh,
  268. u64 new_i_size)
  269. {
  270. int ret;
  271. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  272. handle_t *handle = NULL;
  273. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  274. if (IS_ERR(handle)) {
  275. ret = PTR_ERR(handle);
  276. mlog_errno(ret);
  277. goto out;
  278. }
  279. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  280. new_i_size);
  281. if (ret < 0)
  282. mlog_errno(ret);
  283. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  284. ocfs2_commit_trans(osb, handle);
  285. out:
  286. return ret;
  287. }
  288. static int ocfs2_cow_file_pos(struct inode *inode,
  289. struct buffer_head *fe_bh,
  290. u64 offset)
  291. {
  292. int status;
  293. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  294. unsigned int num_clusters = 0;
  295. unsigned int ext_flags = 0;
  296. /*
  297. * If the new offset is aligned to the range of the cluster, there is
  298. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  299. * CoW either.
  300. */
  301. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  302. return 0;
  303. status = ocfs2_get_clusters(inode, cpos, &phys,
  304. &num_clusters, &ext_flags);
  305. if (status) {
  306. mlog_errno(status);
  307. goto out;
  308. }
  309. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  310. goto out;
  311. return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
  312. out:
  313. return status;
  314. }
  315. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  316. struct inode *inode,
  317. struct buffer_head *fe_bh,
  318. u64 new_i_size)
  319. {
  320. int status;
  321. handle_t *handle;
  322. struct ocfs2_dinode *di;
  323. u64 cluster_bytes;
  324. /*
  325. * We need to CoW the cluster contains the offset if it is reflinked
  326. * since we will call ocfs2_zero_range_for_truncate later which will
  327. * write "0" from offset to the end of the cluster.
  328. */
  329. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  330. if (status) {
  331. mlog_errno(status);
  332. return status;
  333. }
  334. /* TODO: This needs to actually orphan the inode in this
  335. * transaction. */
  336. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  337. if (IS_ERR(handle)) {
  338. status = PTR_ERR(handle);
  339. mlog_errno(status);
  340. goto out;
  341. }
  342. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  343. OCFS2_JOURNAL_ACCESS_WRITE);
  344. if (status < 0) {
  345. mlog_errno(status);
  346. goto out_commit;
  347. }
  348. /*
  349. * Do this before setting i_size.
  350. */
  351. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  352. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  353. cluster_bytes);
  354. if (status) {
  355. mlog_errno(status);
  356. goto out_commit;
  357. }
  358. i_size_write(inode, new_i_size);
  359. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  360. di = (struct ocfs2_dinode *) fe_bh->b_data;
  361. di->i_size = cpu_to_le64(new_i_size);
  362. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  363. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  364. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  365. ocfs2_journal_dirty(handle, fe_bh);
  366. out_commit:
  367. ocfs2_commit_trans(osb, handle);
  368. out:
  369. return status;
  370. }
  371. int ocfs2_truncate_file(struct inode *inode,
  372. struct buffer_head *di_bh,
  373. u64 new_i_size)
  374. {
  375. int status = 0;
  376. struct ocfs2_dinode *fe = NULL;
  377. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  378. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  379. * already validated it */
  380. fe = (struct ocfs2_dinode *) di_bh->b_data;
  381. trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
  382. (unsigned long long)le64_to_cpu(fe->i_size),
  383. (unsigned long long)new_i_size);
  384. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  385. "Inode %llu, inode i_size = %lld != di "
  386. "i_size = %llu, i_flags = 0x%x\n",
  387. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  388. i_size_read(inode),
  389. (unsigned long long)le64_to_cpu(fe->i_size),
  390. le32_to_cpu(fe->i_flags));
  391. if (new_i_size > le64_to_cpu(fe->i_size)) {
  392. trace_ocfs2_truncate_file_error(
  393. (unsigned long long)le64_to_cpu(fe->i_size),
  394. (unsigned long long)new_i_size);
  395. status = -EINVAL;
  396. mlog_errno(status);
  397. goto bail;
  398. }
  399. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  400. ocfs2_resv_discard(&osb->osb_la_resmap,
  401. &OCFS2_I(inode)->ip_la_data_resv);
  402. /*
  403. * The inode lock forced other nodes to sync and drop their
  404. * pages, which (correctly) happens even if we have a truncate
  405. * without allocation change - ocfs2 cluster sizes can be much
  406. * greater than page size, so we have to truncate them
  407. * anyway.
  408. */
  409. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  410. truncate_inode_pages(inode->i_mapping, new_i_size);
  411. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  412. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  413. i_size_read(inode), 1);
  414. if (status)
  415. mlog_errno(status);
  416. goto bail_unlock_sem;
  417. }
  418. /* alright, we're going to need to do a full blown alloc size
  419. * change. Orphan the inode so that recovery can complete the
  420. * truncate if necessary. This does the task of marking
  421. * i_size. */
  422. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  423. if (status < 0) {
  424. mlog_errno(status);
  425. goto bail_unlock_sem;
  426. }
  427. status = ocfs2_commit_truncate(osb, inode, di_bh);
  428. if (status < 0) {
  429. mlog_errno(status);
  430. goto bail_unlock_sem;
  431. }
  432. /* TODO: orphan dir cleanup here. */
  433. bail_unlock_sem:
  434. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  435. bail:
  436. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  437. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  438. return status;
  439. }
  440. /*
  441. * extend file allocation only here.
  442. * we'll update all the disk stuff, and oip->alloc_size
  443. *
  444. * expect stuff to be locked, a transaction started and enough data /
  445. * metadata reservations in the contexts.
  446. *
  447. * Will return -EAGAIN, and a reason if a restart is needed.
  448. * If passed in, *reason will always be set, even in error.
  449. */
  450. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  451. struct inode *inode,
  452. u32 *logical_offset,
  453. u32 clusters_to_add,
  454. int mark_unwritten,
  455. struct buffer_head *fe_bh,
  456. handle_t *handle,
  457. struct ocfs2_alloc_context *data_ac,
  458. struct ocfs2_alloc_context *meta_ac,
  459. enum ocfs2_alloc_restarted *reason_ret)
  460. {
  461. int ret;
  462. struct ocfs2_extent_tree et;
  463. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
  464. ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
  465. clusters_to_add, mark_unwritten,
  466. data_ac, meta_ac, reason_ret);
  467. return ret;
  468. }
  469. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  470. u32 clusters_to_add, int mark_unwritten)
  471. {
  472. int status = 0;
  473. int restart_func = 0;
  474. int credits;
  475. u32 prev_clusters;
  476. struct buffer_head *bh = NULL;
  477. struct ocfs2_dinode *fe = NULL;
  478. handle_t *handle = NULL;
  479. struct ocfs2_alloc_context *data_ac = NULL;
  480. struct ocfs2_alloc_context *meta_ac = NULL;
  481. enum ocfs2_alloc_restarted why = RESTART_NONE;
  482. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  483. struct ocfs2_extent_tree et;
  484. int did_quota = 0;
  485. /*
  486. * Unwritten extent only exists for file systems which
  487. * support holes.
  488. */
  489. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  490. status = ocfs2_read_inode_block(inode, &bh);
  491. if (status < 0) {
  492. mlog_errno(status);
  493. goto leave;
  494. }
  495. fe = (struct ocfs2_dinode *) bh->b_data;
  496. restart_all:
  497. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  498. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  499. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  500. &data_ac, &meta_ac);
  501. if (status) {
  502. mlog_errno(status);
  503. goto leave;
  504. }
  505. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list);
  506. handle = ocfs2_start_trans(osb, credits);
  507. if (IS_ERR(handle)) {
  508. status = PTR_ERR(handle);
  509. handle = NULL;
  510. mlog_errno(status);
  511. goto leave;
  512. }
  513. restarted_transaction:
  514. trace_ocfs2_extend_allocation(
  515. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  516. (unsigned long long)i_size_read(inode),
  517. le32_to_cpu(fe->i_clusters), clusters_to_add,
  518. why, restart_func);
  519. status = dquot_alloc_space_nodirty(inode,
  520. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  521. if (status)
  522. goto leave;
  523. did_quota = 1;
  524. /* reserve a write to the file entry early on - that we if we
  525. * run out of credits in the allocation path, we can still
  526. * update i_size. */
  527. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  528. OCFS2_JOURNAL_ACCESS_WRITE);
  529. if (status < 0) {
  530. mlog_errno(status);
  531. goto leave;
  532. }
  533. prev_clusters = OCFS2_I(inode)->ip_clusters;
  534. status = ocfs2_add_inode_data(osb,
  535. inode,
  536. &logical_start,
  537. clusters_to_add,
  538. mark_unwritten,
  539. bh,
  540. handle,
  541. data_ac,
  542. meta_ac,
  543. &why);
  544. if ((status < 0) && (status != -EAGAIN)) {
  545. if (status != -ENOSPC)
  546. mlog_errno(status);
  547. goto leave;
  548. }
  549. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  550. ocfs2_journal_dirty(handle, bh);
  551. spin_lock(&OCFS2_I(inode)->ip_lock);
  552. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  553. spin_unlock(&OCFS2_I(inode)->ip_lock);
  554. /* Release unused quota reservation */
  555. dquot_free_space(inode,
  556. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  557. did_quota = 0;
  558. if (why != RESTART_NONE && clusters_to_add) {
  559. if (why == RESTART_META) {
  560. restart_func = 1;
  561. status = 0;
  562. } else {
  563. BUG_ON(why != RESTART_TRANS);
  564. status = ocfs2_allocate_extend_trans(handle, 1);
  565. if (status < 0) {
  566. /* handle still has to be committed at
  567. * this point. */
  568. status = -ENOMEM;
  569. mlog_errno(status);
  570. goto leave;
  571. }
  572. goto restarted_transaction;
  573. }
  574. }
  575. trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
  576. le32_to_cpu(fe->i_clusters),
  577. (unsigned long long)le64_to_cpu(fe->i_size),
  578. OCFS2_I(inode)->ip_clusters,
  579. (unsigned long long)i_size_read(inode));
  580. leave:
  581. if (status < 0 && did_quota)
  582. dquot_free_space(inode,
  583. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  584. if (handle) {
  585. ocfs2_commit_trans(osb, handle);
  586. handle = NULL;
  587. }
  588. if (data_ac) {
  589. ocfs2_free_alloc_context(data_ac);
  590. data_ac = NULL;
  591. }
  592. if (meta_ac) {
  593. ocfs2_free_alloc_context(meta_ac);
  594. meta_ac = NULL;
  595. }
  596. if ((!status) && restart_func) {
  597. restart_func = 0;
  598. goto restart_all;
  599. }
  600. brelse(bh);
  601. bh = NULL;
  602. return status;
  603. }
  604. int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  605. u32 clusters_to_add, int mark_unwritten)
  606. {
  607. return __ocfs2_extend_allocation(inode, logical_start,
  608. clusters_to_add, mark_unwritten);
  609. }
  610. /*
  611. * While a write will already be ordering the data, a truncate will not.
  612. * Thus, we need to explicitly order the zeroed pages.
  613. */
  614. static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
  615. struct buffer_head *di_bh)
  616. {
  617. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  618. handle_t *handle = NULL;
  619. int ret = 0;
  620. if (!ocfs2_should_order_data(inode))
  621. goto out;
  622. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  623. if (IS_ERR(handle)) {
  624. ret = -ENOMEM;
  625. mlog_errno(ret);
  626. goto out;
  627. }
  628. ret = ocfs2_jbd2_file_inode(handle, inode);
  629. if (ret < 0) {
  630. mlog_errno(ret);
  631. goto out;
  632. }
  633. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  634. OCFS2_JOURNAL_ACCESS_WRITE);
  635. if (ret)
  636. mlog_errno(ret);
  637. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  638. out:
  639. if (ret) {
  640. if (!IS_ERR(handle))
  641. ocfs2_commit_trans(osb, handle);
  642. handle = ERR_PTR(ret);
  643. }
  644. return handle;
  645. }
  646. /* Some parts of this taken from generic_cont_expand, which turned out
  647. * to be too fragile to do exactly what we need without us having to
  648. * worry about recursive locking in ->write_begin() and ->write_end(). */
  649. static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
  650. u64 abs_to, struct buffer_head *di_bh)
  651. {
  652. struct address_space *mapping = inode->i_mapping;
  653. struct page *page;
  654. unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
  655. handle_t *handle;
  656. int ret = 0;
  657. unsigned zero_from, zero_to, block_start, block_end;
  658. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  659. BUG_ON(abs_from >= abs_to);
  660. BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
  661. BUG_ON(abs_from & (inode->i_blkbits - 1));
  662. handle = ocfs2_zero_start_ordered_transaction(inode, di_bh);
  663. if (IS_ERR(handle)) {
  664. ret = PTR_ERR(handle);
  665. goto out;
  666. }
  667. page = find_or_create_page(mapping, index, GFP_NOFS);
  668. if (!page) {
  669. ret = -ENOMEM;
  670. mlog_errno(ret);
  671. goto out_commit_trans;
  672. }
  673. /* Get the offsets within the page that we want to zero */
  674. zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
  675. zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
  676. if (!zero_to)
  677. zero_to = PAGE_CACHE_SIZE;
  678. trace_ocfs2_write_zero_page(
  679. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  680. (unsigned long long)abs_from,
  681. (unsigned long long)abs_to,
  682. index, zero_from, zero_to);
  683. /* We know that zero_from is block aligned */
  684. for (block_start = zero_from; block_start < zero_to;
  685. block_start = block_end) {
  686. block_end = block_start + (1 << inode->i_blkbits);
  687. /*
  688. * block_start is block-aligned. Bump it by one to force
  689. * __block_write_begin and block_commit_write to zero the
  690. * whole block.
  691. */
  692. ret = __block_write_begin(page, block_start + 1, 0,
  693. ocfs2_get_block);
  694. if (ret < 0) {
  695. mlog_errno(ret);
  696. goto out_unlock;
  697. }
  698. /* must not update i_size! */
  699. ret = block_commit_write(page, block_start + 1,
  700. block_start + 1);
  701. if (ret < 0)
  702. mlog_errno(ret);
  703. else
  704. ret = 0;
  705. }
  706. /*
  707. * fs-writeback will release the dirty pages without page lock
  708. * whose offset are over inode size, the release happens at
  709. * block_write_full_page().
  710. */
  711. i_size_write(inode, abs_to);
  712. inode->i_blocks = ocfs2_inode_sector_count(inode);
  713. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  714. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  715. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  716. di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  717. di->i_mtime_nsec = di->i_ctime_nsec;
  718. if (handle) {
  719. ocfs2_journal_dirty(handle, di_bh);
  720. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  721. }
  722. out_unlock:
  723. unlock_page(page);
  724. page_cache_release(page);
  725. out_commit_trans:
  726. if (handle)
  727. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  728. out:
  729. return ret;
  730. }
  731. /*
  732. * Find the next range to zero. We do this in terms of bytes because
  733. * that's what ocfs2_zero_extend() wants, and it is dealing with the
  734. * pagecache. We may return multiple extents.
  735. *
  736. * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
  737. * needs to be zeroed. range_start and range_end return the next zeroing
  738. * range. A subsequent call should pass the previous range_end as its
  739. * zero_start. If range_end is 0, there's nothing to do.
  740. *
  741. * Unwritten extents are skipped over. Refcounted extents are CoWd.
  742. */
  743. static int ocfs2_zero_extend_get_range(struct inode *inode,
  744. struct buffer_head *di_bh,
  745. u64 zero_start, u64 zero_end,
  746. u64 *range_start, u64 *range_end)
  747. {
  748. int rc = 0, needs_cow = 0;
  749. u32 p_cpos, zero_clusters = 0;
  750. u32 zero_cpos =
  751. zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  752. u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
  753. unsigned int num_clusters = 0;
  754. unsigned int ext_flags = 0;
  755. while (zero_cpos < last_cpos) {
  756. rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
  757. &num_clusters, &ext_flags);
  758. if (rc) {
  759. mlog_errno(rc);
  760. goto out;
  761. }
  762. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  763. zero_clusters = num_clusters;
  764. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  765. needs_cow = 1;
  766. break;
  767. }
  768. zero_cpos += num_clusters;
  769. }
  770. if (!zero_clusters) {
  771. *range_end = 0;
  772. goto out;
  773. }
  774. while ((zero_cpos + zero_clusters) < last_cpos) {
  775. rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
  776. &p_cpos, &num_clusters,
  777. &ext_flags);
  778. if (rc) {
  779. mlog_errno(rc);
  780. goto out;
  781. }
  782. if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
  783. break;
  784. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  785. needs_cow = 1;
  786. zero_clusters += num_clusters;
  787. }
  788. if ((zero_cpos + zero_clusters) > last_cpos)
  789. zero_clusters = last_cpos - zero_cpos;
  790. if (needs_cow) {
  791. rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
  792. zero_clusters, UINT_MAX);
  793. if (rc) {
  794. mlog_errno(rc);
  795. goto out;
  796. }
  797. }
  798. *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
  799. *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
  800. zero_cpos + zero_clusters);
  801. out:
  802. return rc;
  803. }
  804. /*
  805. * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
  806. * has made sure that the entire range needs zeroing.
  807. */
  808. static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
  809. u64 range_end, struct buffer_head *di_bh)
  810. {
  811. int rc = 0;
  812. u64 next_pos;
  813. u64 zero_pos = range_start;
  814. trace_ocfs2_zero_extend_range(
  815. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  816. (unsigned long long)range_start,
  817. (unsigned long long)range_end);
  818. BUG_ON(range_start >= range_end);
  819. while (zero_pos < range_end) {
  820. next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
  821. if (next_pos > range_end)
  822. next_pos = range_end;
  823. rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
  824. if (rc < 0) {
  825. mlog_errno(rc);
  826. break;
  827. }
  828. zero_pos = next_pos;
  829. /*
  830. * Very large extends have the potential to lock up
  831. * the cpu for extended periods of time.
  832. */
  833. cond_resched();
  834. }
  835. return rc;
  836. }
  837. int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
  838. loff_t zero_to_size)
  839. {
  840. int ret = 0;
  841. u64 zero_start, range_start = 0, range_end = 0;
  842. struct super_block *sb = inode->i_sb;
  843. zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  844. trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
  845. (unsigned long long)zero_start,
  846. (unsigned long long)i_size_read(inode));
  847. while (zero_start < zero_to_size) {
  848. ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
  849. zero_to_size,
  850. &range_start,
  851. &range_end);
  852. if (ret) {
  853. mlog_errno(ret);
  854. break;
  855. }
  856. if (!range_end)
  857. break;
  858. /* Trim the ends */
  859. if (range_start < zero_start)
  860. range_start = zero_start;
  861. if (range_end > zero_to_size)
  862. range_end = zero_to_size;
  863. ret = ocfs2_zero_extend_range(inode, range_start,
  864. range_end, di_bh);
  865. if (ret) {
  866. mlog_errno(ret);
  867. break;
  868. }
  869. zero_start = range_end;
  870. }
  871. return ret;
  872. }
  873. int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
  874. u64 new_i_size, u64 zero_to)
  875. {
  876. int ret;
  877. u32 clusters_to_add;
  878. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  879. /*
  880. * Only quota files call this without a bh, and they can't be
  881. * refcounted.
  882. */
  883. BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  884. BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
  885. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  886. if (clusters_to_add < oi->ip_clusters)
  887. clusters_to_add = 0;
  888. else
  889. clusters_to_add -= oi->ip_clusters;
  890. if (clusters_to_add) {
  891. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  892. clusters_to_add, 0);
  893. if (ret) {
  894. mlog_errno(ret);
  895. goto out;
  896. }
  897. }
  898. /*
  899. * Call this even if we don't add any clusters to the tree. We
  900. * still need to zero the area between the old i_size and the
  901. * new i_size.
  902. */
  903. ret = ocfs2_zero_extend(inode, di_bh, zero_to);
  904. if (ret < 0)
  905. mlog_errno(ret);
  906. out:
  907. return ret;
  908. }
  909. static int ocfs2_extend_file(struct inode *inode,
  910. struct buffer_head *di_bh,
  911. u64 new_i_size)
  912. {
  913. int ret = 0;
  914. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  915. BUG_ON(!di_bh);
  916. /* setattr sometimes calls us like this. */
  917. if (new_i_size == 0)
  918. goto out;
  919. if (i_size_read(inode) == new_i_size)
  920. goto out;
  921. BUG_ON(new_i_size < i_size_read(inode));
  922. /*
  923. * The alloc sem blocks people in read/write from reading our
  924. * allocation until we're done changing it. We depend on
  925. * i_mutex to block other extend/truncate calls while we're
  926. * here. We even have to hold it for sparse files because there
  927. * might be some tail zeroing.
  928. */
  929. down_write(&oi->ip_alloc_sem);
  930. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  931. /*
  932. * We can optimize small extends by keeping the inodes
  933. * inline data.
  934. */
  935. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  936. up_write(&oi->ip_alloc_sem);
  937. goto out_update_size;
  938. }
  939. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  940. if (ret) {
  941. up_write(&oi->ip_alloc_sem);
  942. mlog_errno(ret);
  943. goto out;
  944. }
  945. }
  946. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  947. ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
  948. else
  949. ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
  950. new_i_size);
  951. up_write(&oi->ip_alloc_sem);
  952. if (ret < 0) {
  953. mlog_errno(ret);
  954. goto out;
  955. }
  956. out_update_size:
  957. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  958. if (ret < 0)
  959. mlog_errno(ret);
  960. out:
  961. return ret;
  962. }
  963. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  964. {
  965. int status = 0, size_change;
  966. struct inode *inode = d_inode(dentry);
  967. struct super_block *sb = inode->i_sb;
  968. struct ocfs2_super *osb = OCFS2_SB(sb);
  969. struct buffer_head *bh = NULL;
  970. handle_t *handle = NULL;
  971. struct dquot *transfer_to[MAXQUOTAS] = { };
  972. int qtype;
  973. trace_ocfs2_setattr(inode, dentry,
  974. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  975. dentry->d_name.len, dentry->d_name.name,
  976. attr->ia_valid, attr->ia_mode,
  977. from_kuid(&init_user_ns, attr->ia_uid),
  978. from_kgid(&init_user_ns, attr->ia_gid));
  979. /* ensuring we don't even attempt to truncate a symlink */
  980. if (S_ISLNK(inode->i_mode))
  981. attr->ia_valid &= ~ATTR_SIZE;
  982. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  983. | ATTR_GID | ATTR_UID | ATTR_MODE)
  984. if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
  985. return 0;
  986. status = inode_change_ok(inode, attr);
  987. if (status)
  988. return status;
  989. if (is_quota_modification(inode, attr))
  990. dquot_initialize(inode);
  991. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  992. if (size_change) {
  993. status = ocfs2_rw_lock(inode, 1);
  994. if (status < 0) {
  995. mlog_errno(status);
  996. goto bail;
  997. }
  998. }
  999. status = ocfs2_inode_lock(inode, &bh, 1);
  1000. if (status < 0) {
  1001. if (status != -ENOENT)
  1002. mlog_errno(status);
  1003. goto bail_unlock_rw;
  1004. }
  1005. if (size_change) {
  1006. status = inode_newsize_ok(inode, attr->ia_size);
  1007. if (status)
  1008. goto bail_unlock;
  1009. inode_dio_wait(inode);
  1010. if (i_size_read(inode) >= attr->ia_size) {
  1011. if (ocfs2_should_order_data(inode)) {
  1012. status = ocfs2_begin_ordered_truncate(inode,
  1013. attr->ia_size);
  1014. if (status)
  1015. goto bail_unlock;
  1016. }
  1017. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  1018. } else
  1019. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  1020. if (status < 0) {
  1021. if (status != -ENOSPC)
  1022. mlog_errno(status);
  1023. status = -ENOSPC;
  1024. goto bail_unlock;
  1025. }
  1026. }
  1027. if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  1028. (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  1029. /*
  1030. * Gather pointers to quota structures so that allocation /
  1031. * freeing of quota structures happens here and not inside
  1032. * dquot_transfer() where we have problems with lock ordering
  1033. */
  1034. if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
  1035. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1036. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1037. transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
  1038. if (!transfer_to[USRQUOTA]) {
  1039. status = -ESRCH;
  1040. goto bail_unlock;
  1041. }
  1042. }
  1043. if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
  1044. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1045. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1046. transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
  1047. if (!transfer_to[GRPQUOTA]) {
  1048. status = -ESRCH;
  1049. goto bail_unlock;
  1050. }
  1051. }
  1052. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  1053. 2 * ocfs2_quota_trans_credits(sb));
  1054. if (IS_ERR(handle)) {
  1055. status = PTR_ERR(handle);
  1056. mlog_errno(status);
  1057. goto bail_unlock;
  1058. }
  1059. status = __dquot_transfer(inode, transfer_to);
  1060. if (status < 0)
  1061. goto bail_commit;
  1062. } else {
  1063. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1064. if (IS_ERR(handle)) {
  1065. status = PTR_ERR(handle);
  1066. mlog_errno(status);
  1067. goto bail_unlock;
  1068. }
  1069. }
  1070. setattr_copy(inode, attr);
  1071. mark_inode_dirty(inode);
  1072. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  1073. if (status < 0)
  1074. mlog_errno(status);
  1075. bail_commit:
  1076. ocfs2_commit_trans(osb, handle);
  1077. bail_unlock:
  1078. ocfs2_inode_unlock(inode, 1);
  1079. bail_unlock_rw:
  1080. if (size_change)
  1081. ocfs2_rw_unlock(inode, 1);
  1082. bail:
  1083. brelse(bh);
  1084. /* Release quota pointers in case we acquired them */
  1085. for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++)
  1086. dqput(transfer_to[qtype]);
  1087. if (!status && attr->ia_valid & ATTR_MODE) {
  1088. status = posix_acl_chmod(inode, inode->i_mode);
  1089. if (status < 0)
  1090. mlog_errno(status);
  1091. }
  1092. return status;
  1093. }
  1094. int ocfs2_getattr(struct vfsmount *mnt,
  1095. struct dentry *dentry,
  1096. struct kstat *stat)
  1097. {
  1098. struct inode *inode = d_inode(dentry);
  1099. struct super_block *sb = d_inode(dentry)->i_sb;
  1100. struct ocfs2_super *osb = sb->s_fs_info;
  1101. int err;
  1102. err = ocfs2_inode_revalidate(dentry);
  1103. if (err) {
  1104. if (err != -ENOENT)
  1105. mlog_errno(err);
  1106. goto bail;
  1107. }
  1108. generic_fillattr(inode, stat);
  1109. /* We set the blksize from the cluster size for performance */
  1110. stat->blksize = osb->s_clustersize;
  1111. bail:
  1112. return err;
  1113. }
  1114. int ocfs2_permission(struct inode *inode, int mask)
  1115. {
  1116. int ret;
  1117. if (mask & MAY_NOT_BLOCK)
  1118. return -ECHILD;
  1119. ret = ocfs2_inode_lock(inode, NULL, 0);
  1120. if (ret) {
  1121. if (ret != -ENOENT)
  1122. mlog_errno(ret);
  1123. goto out;
  1124. }
  1125. ret = generic_permission(inode, mask);
  1126. ocfs2_inode_unlock(inode, 0);
  1127. out:
  1128. return ret;
  1129. }
  1130. static int __ocfs2_write_remove_suid(struct inode *inode,
  1131. struct buffer_head *bh)
  1132. {
  1133. int ret;
  1134. handle_t *handle;
  1135. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1136. struct ocfs2_dinode *di;
  1137. trace_ocfs2_write_remove_suid(
  1138. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1139. inode->i_mode);
  1140. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1141. if (IS_ERR(handle)) {
  1142. ret = PTR_ERR(handle);
  1143. mlog_errno(ret);
  1144. goto out;
  1145. }
  1146. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1147. OCFS2_JOURNAL_ACCESS_WRITE);
  1148. if (ret < 0) {
  1149. mlog_errno(ret);
  1150. goto out_trans;
  1151. }
  1152. inode->i_mode &= ~S_ISUID;
  1153. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1154. inode->i_mode &= ~S_ISGID;
  1155. di = (struct ocfs2_dinode *) bh->b_data;
  1156. di->i_mode = cpu_to_le16(inode->i_mode);
  1157. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  1158. ocfs2_journal_dirty(handle, bh);
  1159. out_trans:
  1160. ocfs2_commit_trans(osb, handle);
  1161. out:
  1162. return ret;
  1163. }
  1164. /*
  1165. * Will look for holes and unwritten extents in the range starting at
  1166. * pos for count bytes (inclusive).
  1167. */
  1168. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1169. size_t count)
  1170. {
  1171. int ret = 0;
  1172. unsigned int extent_flags;
  1173. u32 cpos, clusters, extent_len, phys_cpos;
  1174. struct super_block *sb = inode->i_sb;
  1175. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1176. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1177. while (clusters) {
  1178. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1179. &extent_flags);
  1180. if (ret < 0) {
  1181. mlog_errno(ret);
  1182. goto out;
  1183. }
  1184. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1185. ret = 1;
  1186. break;
  1187. }
  1188. if (extent_len > clusters)
  1189. extent_len = clusters;
  1190. clusters -= extent_len;
  1191. cpos += extent_len;
  1192. }
  1193. out:
  1194. return ret;
  1195. }
  1196. static int ocfs2_write_remove_suid(struct inode *inode)
  1197. {
  1198. int ret;
  1199. struct buffer_head *bh = NULL;
  1200. ret = ocfs2_read_inode_block(inode, &bh);
  1201. if (ret < 0) {
  1202. mlog_errno(ret);
  1203. goto out;
  1204. }
  1205. ret = __ocfs2_write_remove_suid(inode, bh);
  1206. out:
  1207. brelse(bh);
  1208. return ret;
  1209. }
  1210. /*
  1211. * Allocate enough extents to cover the region starting at byte offset
  1212. * start for len bytes. Existing extents are skipped, any extents
  1213. * added are marked as "unwritten".
  1214. */
  1215. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1216. u64 start, u64 len)
  1217. {
  1218. int ret;
  1219. u32 cpos, phys_cpos, clusters, alloc_size;
  1220. u64 end = start + len;
  1221. struct buffer_head *di_bh = NULL;
  1222. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1223. ret = ocfs2_read_inode_block(inode, &di_bh);
  1224. if (ret) {
  1225. mlog_errno(ret);
  1226. goto out;
  1227. }
  1228. /*
  1229. * Nothing to do if the requested reservation range
  1230. * fits within the inode.
  1231. */
  1232. if (ocfs2_size_fits_inline_data(di_bh, end))
  1233. goto out;
  1234. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1235. if (ret) {
  1236. mlog_errno(ret);
  1237. goto out;
  1238. }
  1239. }
  1240. /*
  1241. * We consider both start and len to be inclusive.
  1242. */
  1243. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1244. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1245. clusters -= cpos;
  1246. while (clusters) {
  1247. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1248. &alloc_size, NULL);
  1249. if (ret) {
  1250. mlog_errno(ret);
  1251. goto out;
  1252. }
  1253. /*
  1254. * Hole or existing extent len can be arbitrary, so
  1255. * cap it to our own allocation request.
  1256. */
  1257. if (alloc_size > clusters)
  1258. alloc_size = clusters;
  1259. if (phys_cpos) {
  1260. /*
  1261. * We already have an allocation at this
  1262. * region so we can safely skip it.
  1263. */
  1264. goto next;
  1265. }
  1266. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1267. if (ret) {
  1268. if (ret != -ENOSPC)
  1269. mlog_errno(ret);
  1270. goto out;
  1271. }
  1272. next:
  1273. cpos += alloc_size;
  1274. clusters -= alloc_size;
  1275. }
  1276. ret = 0;
  1277. out:
  1278. brelse(di_bh);
  1279. return ret;
  1280. }
  1281. /*
  1282. * Truncate a byte range, avoiding pages within partial clusters. This
  1283. * preserves those pages for the zeroing code to write to.
  1284. */
  1285. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1286. u64 byte_len)
  1287. {
  1288. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1289. loff_t start, end;
  1290. struct address_space *mapping = inode->i_mapping;
  1291. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1292. end = byte_start + byte_len;
  1293. end = end & ~(osb->s_clustersize - 1);
  1294. if (start < end) {
  1295. unmap_mapping_range(mapping, start, end - start, 0);
  1296. truncate_inode_pages_range(mapping, start, end - 1);
  1297. }
  1298. }
  1299. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1300. u64 start, u64 len)
  1301. {
  1302. int ret = 0;
  1303. u64 tmpend, end = start + len;
  1304. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1305. unsigned int csize = osb->s_clustersize;
  1306. handle_t *handle;
  1307. /*
  1308. * The "start" and "end" values are NOT necessarily part of
  1309. * the range whose allocation is being deleted. Rather, this
  1310. * is what the user passed in with the request. We must zero
  1311. * partial clusters here. There's no need to worry about
  1312. * physical allocation - the zeroing code knows to skip holes.
  1313. */
  1314. trace_ocfs2_zero_partial_clusters(
  1315. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1316. (unsigned long long)start, (unsigned long long)end);
  1317. /*
  1318. * If both edges are on a cluster boundary then there's no
  1319. * zeroing required as the region is part of the allocation to
  1320. * be truncated.
  1321. */
  1322. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1323. goto out;
  1324. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1325. if (IS_ERR(handle)) {
  1326. ret = PTR_ERR(handle);
  1327. mlog_errno(ret);
  1328. goto out;
  1329. }
  1330. /*
  1331. * We want to get the byte offset of the end of the 1st cluster.
  1332. */
  1333. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1334. if (tmpend > end)
  1335. tmpend = end;
  1336. trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start,
  1337. (unsigned long long)tmpend);
  1338. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1339. if (ret)
  1340. mlog_errno(ret);
  1341. if (tmpend < end) {
  1342. /*
  1343. * This may make start and end equal, but the zeroing
  1344. * code will skip any work in that case so there's no
  1345. * need to catch it up here.
  1346. */
  1347. start = end & ~(osb->s_clustersize - 1);
  1348. trace_ocfs2_zero_partial_clusters_range2(
  1349. (unsigned long long)start, (unsigned long long)end);
  1350. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1351. if (ret)
  1352. mlog_errno(ret);
  1353. }
  1354. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1355. ocfs2_commit_trans(osb, handle);
  1356. out:
  1357. return ret;
  1358. }
  1359. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1360. {
  1361. int i;
  1362. struct ocfs2_extent_rec *rec = NULL;
  1363. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1364. rec = &el->l_recs[i];
  1365. if (le32_to_cpu(rec->e_cpos) < pos)
  1366. break;
  1367. }
  1368. return i;
  1369. }
  1370. /*
  1371. * Helper to calculate the punching pos and length in one run, we handle the
  1372. * following three cases in order:
  1373. *
  1374. * - remove the entire record
  1375. * - remove a partial record
  1376. * - no record needs to be removed (hole-punching completed)
  1377. */
  1378. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1379. struct ocfs2_extent_list *el,
  1380. struct ocfs2_extent_rec *rec,
  1381. u32 trunc_start, u32 *trunc_cpos,
  1382. u32 *trunc_len, u32 *trunc_end,
  1383. u64 *blkno, int *done)
  1384. {
  1385. int ret = 0;
  1386. u32 coff, range;
  1387. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1388. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1389. /*
  1390. * remove an entire extent record.
  1391. */
  1392. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1393. /*
  1394. * Skip holes if any.
  1395. */
  1396. if (range < *trunc_end)
  1397. *trunc_end = range;
  1398. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1399. *blkno = le64_to_cpu(rec->e_blkno);
  1400. *trunc_end = le32_to_cpu(rec->e_cpos);
  1401. } else if (range > trunc_start) {
  1402. /*
  1403. * remove a partial extent record, which means we're
  1404. * removing the last extent record.
  1405. */
  1406. *trunc_cpos = trunc_start;
  1407. /*
  1408. * skip hole if any.
  1409. */
  1410. if (range < *trunc_end)
  1411. *trunc_end = range;
  1412. *trunc_len = *trunc_end - trunc_start;
  1413. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1414. *blkno = le64_to_cpu(rec->e_blkno) +
  1415. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1416. *trunc_end = trunc_start;
  1417. } else {
  1418. /*
  1419. * It may have two following possibilities:
  1420. *
  1421. * - last record has been removed
  1422. * - trunc_start was within a hole
  1423. *
  1424. * both two cases mean the completion of hole punching.
  1425. */
  1426. ret = 1;
  1427. }
  1428. *done = ret;
  1429. }
  1430. static int ocfs2_remove_inode_range(struct inode *inode,
  1431. struct buffer_head *di_bh, u64 byte_start,
  1432. u64 byte_len)
  1433. {
  1434. int ret = 0, flags = 0, done = 0, i;
  1435. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1436. u32 cluster_in_el;
  1437. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1438. struct ocfs2_cached_dealloc_ctxt dealloc;
  1439. struct address_space *mapping = inode->i_mapping;
  1440. struct ocfs2_extent_tree et;
  1441. struct ocfs2_path *path = NULL;
  1442. struct ocfs2_extent_list *el = NULL;
  1443. struct ocfs2_extent_rec *rec = NULL;
  1444. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1445. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1446. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1447. ocfs2_init_dealloc_ctxt(&dealloc);
  1448. trace_ocfs2_remove_inode_range(
  1449. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1450. (unsigned long long)byte_start,
  1451. (unsigned long long)byte_len);
  1452. if (byte_len == 0)
  1453. return 0;
  1454. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1455. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1456. byte_start + byte_len, 0);
  1457. if (ret) {
  1458. mlog_errno(ret);
  1459. goto out;
  1460. }
  1461. /*
  1462. * There's no need to get fancy with the page cache
  1463. * truncate of an inline-data inode. We're talking
  1464. * about less than a page here, which will be cached
  1465. * in the dinode buffer anyway.
  1466. */
  1467. unmap_mapping_range(mapping, 0, 0, 0);
  1468. truncate_inode_pages(mapping, 0);
  1469. goto out;
  1470. }
  1471. /*
  1472. * For reflinks, we may need to CoW 2 clusters which might be
  1473. * partially zero'd later, if hole's start and end offset were
  1474. * within one cluster(means is not exactly aligned to clustersize).
  1475. */
  1476. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  1477. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1478. if (ret) {
  1479. mlog_errno(ret);
  1480. goto out;
  1481. }
  1482. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1483. if (ret) {
  1484. mlog_errno(ret);
  1485. goto out;
  1486. }
  1487. }
  1488. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1489. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1490. cluster_in_el = trunc_end;
  1491. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1492. if (ret) {
  1493. mlog_errno(ret);
  1494. goto out;
  1495. }
  1496. path = ocfs2_new_path_from_et(&et);
  1497. if (!path) {
  1498. ret = -ENOMEM;
  1499. mlog_errno(ret);
  1500. goto out;
  1501. }
  1502. while (trunc_end > trunc_start) {
  1503. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1504. cluster_in_el);
  1505. if (ret) {
  1506. mlog_errno(ret);
  1507. goto out;
  1508. }
  1509. el = path_leaf_el(path);
  1510. i = ocfs2_find_rec(el, trunc_end);
  1511. /*
  1512. * Need to go to previous extent block.
  1513. */
  1514. if (i < 0) {
  1515. if (path->p_tree_depth == 0)
  1516. break;
  1517. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1518. path,
  1519. &cluster_in_el);
  1520. if (ret) {
  1521. mlog_errno(ret);
  1522. goto out;
  1523. }
  1524. /*
  1525. * We've reached the leftmost extent block,
  1526. * it's safe to leave.
  1527. */
  1528. if (cluster_in_el == 0)
  1529. break;
  1530. /*
  1531. * The 'pos' searched for previous extent block is
  1532. * always one cluster less than actual trunc_end.
  1533. */
  1534. trunc_end = cluster_in_el + 1;
  1535. ocfs2_reinit_path(path, 1);
  1536. continue;
  1537. } else
  1538. rec = &el->l_recs[i];
  1539. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1540. &trunc_len, &trunc_end, &blkno, &done);
  1541. if (done)
  1542. break;
  1543. flags = rec->e_flags;
  1544. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1545. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1546. phys_cpos, trunc_len, flags,
  1547. &dealloc, refcount_loc, false);
  1548. if (ret < 0) {
  1549. mlog_errno(ret);
  1550. goto out;
  1551. }
  1552. cluster_in_el = trunc_end;
  1553. ocfs2_reinit_path(path, 1);
  1554. }
  1555. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1556. out:
  1557. ocfs2_free_path(path);
  1558. ocfs2_schedule_truncate_log_flush(osb, 1);
  1559. ocfs2_run_deallocs(osb, &dealloc);
  1560. return ret;
  1561. }
  1562. /*
  1563. * Parts of this function taken from xfs_change_file_space()
  1564. */
  1565. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1566. loff_t f_pos, unsigned int cmd,
  1567. struct ocfs2_space_resv *sr,
  1568. int change_size)
  1569. {
  1570. int ret;
  1571. s64 llen;
  1572. loff_t size;
  1573. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1574. struct buffer_head *di_bh = NULL;
  1575. handle_t *handle;
  1576. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1577. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1578. return -EROFS;
  1579. mutex_lock(&inode->i_mutex);
  1580. /*
  1581. * This prevents concurrent writes on other nodes
  1582. */
  1583. ret = ocfs2_rw_lock(inode, 1);
  1584. if (ret) {
  1585. mlog_errno(ret);
  1586. goto out;
  1587. }
  1588. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1589. if (ret) {
  1590. mlog_errno(ret);
  1591. goto out_rw_unlock;
  1592. }
  1593. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1594. ret = -EPERM;
  1595. goto out_inode_unlock;
  1596. }
  1597. switch (sr->l_whence) {
  1598. case 0: /*SEEK_SET*/
  1599. break;
  1600. case 1: /*SEEK_CUR*/
  1601. sr->l_start += f_pos;
  1602. break;
  1603. case 2: /*SEEK_END*/
  1604. sr->l_start += i_size_read(inode);
  1605. break;
  1606. default:
  1607. ret = -EINVAL;
  1608. goto out_inode_unlock;
  1609. }
  1610. sr->l_whence = 0;
  1611. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1612. if (sr->l_start < 0
  1613. || sr->l_start > max_off
  1614. || (sr->l_start + llen) < 0
  1615. || (sr->l_start + llen) > max_off) {
  1616. ret = -EINVAL;
  1617. goto out_inode_unlock;
  1618. }
  1619. size = sr->l_start + sr->l_len;
  1620. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
  1621. cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
  1622. if (sr->l_len <= 0) {
  1623. ret = -EINVAL;
  1624. goto out_inode_unlock;
  1625. }
  1626. }
  1627. if (file && should_remove_suid(file->f_path.dentry)) {
  1628. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1629. if (ret) {
  1630. mlog_errno(ret);
  1631. goto out_inode_unlock;
  1632. }
  1633. }
  1634. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1635. switch (cmd) {
  1636. case OCFS2_IOC_RESVSP:
  1637. case OCFS2_IOC_RESVSP64:
  1638. /*
  1639. * This takes unsigned offsets, but the signed ones we
  1640. * pass have been checked against overflow above.
  1641. */
  1642. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1643. sr->l_len);
  1644. break;
  1645. case OCFS2_IOC_UNRESVSP:
  1646. case OCFS2_IOC_UNRESVSP64:
  1647. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1648. sr->l_len);
  1649. break;
  1650. default:
  1651. ret = -EINVAL;
  1652. }
  1653. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1654. if (ret) {
  1655. mlog_errno(ret);
  1656. goto out_inode_unlock;
  1657. }
  1658. /*
  1659. * We update c/mtime for these changes
  1660. */
  1661. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1662. if (IS_ERR(handle)) {
  1663. ret = PTR_ERR(handle);
  1664. mlog_errno(ret);
  1665. goto out_inode_unlock;
  1666. }
  1667. if (change_size && i_size_read(inode) < size)
  1668. i_size_write(inode, size);
  1669. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1670. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1671. if (ret < 0)
  1672. mlog_errno(ret);
  1673. if (file && (file->f_flags & O_SYNC))
  1674. handle->h_sync = 1;
  1675. ocfs2_commit_trans(osb, handle);
  1676. out_inode_unlock:
  1677. brelse(di_bh);
  1678. ocfs2_inode_unlock(inode, 1);
  1679. out_rw_unlock:
  1680. ocfs2_rw_unlock(inode, 1);
  1681. out:
  1682. mutex_unlock(&inode->i_mutex);
  1683. return ret;
  1684. }
  1685. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1686. struct ocfs2_space_resv *sr)
  1687. {
  1688. struct inode *inode = file_inode(file);
  1689. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1690. int ret;
  1691. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1692. !ocfs2_writes_unwritten_extents(osb))
  1693. return -ENOTTY;
  1694. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1695. !ocfs2_sparse_alloc(osb))
  1696. return -ENOTTY;
  1697. if (!S_ISREG(inode->i_mode))
  1698. return -EINVAL;
  1699. if (!(file->f_mode & FMODE_WRITE))
  1700. return -EBADF;
  1701. ret = mnt_want_write_file(file);
  1702. if (ret)
  1703. return ret;
  1704. ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1705. mnt_drop_write_file(file);
  1706. return ret;
  1707. }
  1708. static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
  1709. loff_t len)
  1710. {
  1711. struct inode *inode = file_inode(file);
  1712. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1713. struct ocfs2_space_resv sr;
  1714. int change_size = 1;
  1715. int cmd = OCFS2_IOC_RESVSP64;
  1716. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1717. return -EOPNOTSUPP;
  1718. if (!ocfs2_writes_unwritten_extents(osb))
  1719. return -EOPNOTSUPP;
  1720. if (mode & FALLOC_FL_KEEP_SIZE)
  1721. change_size = 0;
  1722. if (mode & FALLOC_FL_PUNCH_HOLE)
  1723. cmd = OCFS2_IOC_UNRESVSP64;
  1724. sr.l_whence = 0;
  1725. sr.l_start = (s64)offset;
  1726. sr.l_len = (s64)len;
  1727. return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
  1728. change_size);
  1729. }
  1730. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1731. size_t count)
  1732. {
  1733. int ret = 0;
  1734. unsigned int extent_flags;
  1735. u32 cpos, clusters, extent_len, phys_cpos;
  1736. struct super_block *sb = inode->i_sb;
  1737. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1738. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1739. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1740. return 0;
  1741. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1742. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1743. while (clusters) {
  1744. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1745. &extent_flags);
  1746. if (ret < 0) {
  1747. mlog_errno(ret);
  1748. goto out;
  1749. }
  1750. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1751. ret = 1;
  1752. break;
  1753. }
  1754. if (extent_len > clusters)
  1755. extent_len = clusters;
  1756. clusters -= extent_len;
  1757. cpos += extent_len;
  1758. }
  1759. out:
  1760. return ret;
  1761. }
  1762. static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
  1763. {
  1764. int blockmask = inode->i_sb->s_blocksize - 1;
  1765. loff_t final_size = pos + count;
  1766. if ((pos & blockmask) || (final_size & blockmask))
  1767. return 1;
  1768. return 0;
  1769. }
  1770. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1771. struct file *file,
  1772. loff_t pos, size_t count,
  1773. int *meta_level)
  1774. {
  1775. int ret;
  1776. struct buffer_head *di_bh = NULL;
  1777. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1778. u32 clusters =
  1779. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1780. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1781. if (ret) {
  1782. mlog_errno(ret);
  1783. goto out;
  1784. }
  1785. *meta_level = 1;
  1786. ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
  1787. if (ret)
  1788. mlog_errno(ret);
  1789. out:
  1790. brelse(di_bh);
  1791. return ret;
  1792. }
  1793. static int ocfs2_prepare_inode_for_write(struct file *file,
  1794. loff_t pos,
  1795. size_t count,
  1796. int appending,
  1797. int *direct_io,
  1798. int *has_refcount)
  1799. {
  1800. int ret = 0, meta_level = 0;
  1801. struct dentry *dentry = file->f_path.dentry;
  1802. struct inode *inode = d_inode(dentry);
  1803. loff_t end;
  1804. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1805. int full_coherency = !(osb->s_mount_opt &
  1806. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1807. /*
  1808. * We start with a read level meta lock and only jump to an ex
  1809. * if we need to make modifications here.
  1810. */
  1811. for(;;) {
  1812. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1813. if (ret < 0) {
  1814. meta_level = -1;
  1815. mlog_errno(ret);
  1816. goto out;
  1817. }
  1818. /* Clear suid / sgid if necessary. We do this here
  1819. * instead of later in the write path because
  1820. * remove_suid() calls ->setattr without any hint that
  1821. * we may have already done our cluster locking. Since
  1822. * ocfs2_setattr() *must* take cluster locks to
  1823. * proceed, this will lead us to recursively lock the
  1824. * inode. There's also the dinode i_size state which
  1825. * can be lost via setattr during extending writes (we
  1826. * set inode->i_size at the end of a write. */
  1827. if (should_remove_suid(dentry)) {
  1828. if (meta_level == 0) {
  1829. ocfs2_inode_unlock(inode, meta_level);
  1830. meta_level = 1;
  1831. continue;
  1832. }
  1833. ret = ocfs2_write_remove_suid(inode);
  1834. if (ret < 0) {
  1835. mlog_errno(ret);
  1836. goto out_unlock;
  1837. }
  1838. }
  1839. end = pos + count;
  1840. ret = ocfs2_check_range_for_refcount(inode, pos, count);
  1841. if (ret == 1) {
  1842. ocfs2_inode_unlock(inode, meta_level);
  1843. meta_level = -1;
  1844. ret = ocfs2_prepare_inode_for_refcount(inode,
  1845. file,
  1846. pos,
  1847. count,
  1848. &meta_level);
  1849. if (has_refcount)
  1850. *has_refcount = 1;
  1851. if (direct_io)
  1852. *direct_io = 0;
  1853. }
  1854. if (ret < 0) {
  1855. mlog_errno(ret);
  1856. goto out_unlock;
  1857. }
  1858. /*
  1859. * Skip the O_DIRECT checks if we don't need
  1860. * them.
  1861. */
  1862. if (!direct_io || !(*direct_io))
  1863. break;
  1864. /*
  1865. * There's no sane way to do direct writes to an inode
  1866. * with inline data.
  1867. */
  1868. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1869. *direct_io = 0;
  1870. break;
  1871. }
  1872. /*
  1873. * Allowing concurrent direct writes means
  1874. * i_size changes wouldn't be synchronized, so
  1875. * one node could wind up truncating another
  1876. * nodes writes.
  1877. */
  1878. if (end > i_size_read(inode) && !full_coherency) {
  1879. *direct_io = 0;
  1880. break;
  1881. }
  1882. /*
  1883. * Fallback to old way if the feature bit is not set.
  1884. */
  1885. if (end > i_size_read(inode) &&
  1886. !ocfs2_supports_append_dio(osb)) {
  1887. *direct_io = 0;
  1888. break;
  1889. }
  1890. /*
  1891. * We don't fill holes during direct io, so
  1892. * check for them here. If any are found, the
  1893. * caller will have to retake some cluster
  1894. * locks and initiate the io as buffered.
  1895. */
  1896. ret = ocfs2_check_range_for_holes(inode, pos, count);
  1897. if (ret == 1) {
  1898. /*
  1899. * Fallback to old way if the feature bit is not set.
  1900. * Otherwise try dio first and then complete the rest
  1901. * request through buffer io.
  1902. */
  1903. if (!ocfs2_supports_append_dio(osb))
  1904. *direct_io = 0;
  1905. ret = 0;
  1906. } else if (ret < 0)
  1907. mlog_errno(ret);
  1908. break;
  1909. }
  1910. out_unlock:
  1911. trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
  1912. pos, appending, count,
  1913. direct_io, has_refcount);
  1914. if (meta_level >= 0)
  1915. ocfs2_inode_unlock(inode, meta_level);
  1916. out:
  1917. return ret;
  1918. }
  1919. static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
  1920. struct iov_iter *from)
  1921. {
  1922. int direct_io, appending, rw_level, have_alloc_sem = 0;
  1923. int can_do_direct, has_refcount = 0;
  1924. ssize_t written = 0;
  1925. ssize_t ret;
  1926. size_t count = iov_iter_count(from), orig_count;
  1927. loff_t old_size;
  1928. u32 old_clusters;
  1929. struct file *file = iocb->ki_filp;
  1930. struct inode *inode = file_inode(file);
  1931. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1932. int full_coherency = !(osb->s_mount_opt &
  1933. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1934. int unaligned_dio = 0;
  1935. int dropped_dio = 0;
  1936. trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
  1937. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1938. file->f_path.dentry->d_name.len,
  1939. file->f_path.dentry->d_name.name,
  1940. (unsigned int)from->nr_segs); /* GRRRRR */
  1941. if (count == 0)
  1942. return 0;
  1943. appending = iocb->ki_flags & IOCB_APPEND ? 1 : 0;
  1944. direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
  1945. mutex_lock(&inode->i_mutex);
  1946. ocfs2_iocb_clear_sem_locked(iocb);
  1947. relock:
  1948. /* to match setattr's i_mutex -> rw_lock ordering */
  1949. if (direct_io) {
  1950. have_alloc_sem = 1;
  1951. /* communicate with ocfs2_dio_end_io */
  1952. ocfs2_iocb_set_sem_locked(iocb);
  1953. }
  1954. /*
  1955. * Concurrent O_DIRECT writes are allowed with
  1956. * mount_option "coherency=buffered".
  1957. */
  1958. rw_level = (!direct_io || full_coherency);
  1959. ret = ocfs2_rw_lock(inode, rw_level);
  1960. if (ret < 0) {
  1961. mlog_errno(ret);
  1962. goto out_sems;
  1963. }
  1964. /*
  1965. * O_DIRECT writes with "coherency=full" need to take EX cluster
  1966. * inode_lock to guarantee coherency.
  1967. */
  1968. if (direct_io && full_coherency) {
  1969. /*
  1970. * We need to take and drop the inode lock to force
  1971. * other nodes to drop their caches. Buffered I/O
  1972. * already does this in write_begin().
  1973. */
  1974. ret = ocfs2_inode_lock(inode, NULL, 1);
  1975. if (ret < 0) {
  1976. mlog_errno(ret);
  1977. goto out;
  1978. }
  1979. ocfs2_inode_unlock(inode, 1);
  1980. }
  1981. orig_count = iov_iter_count(from);
  1982. ret = generic_write_checks(iocb, from);
  1983. if (ret <= 0) {
  1984. if (ret)
  1985. mlog_errno(ret);
  1986. goto out;
  1987. }
  1988. count = ret;
  1989. can_do_direct = direct_io;
  1990. ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, appending,
  1991. &can_do_direct, &has_refcount);
  1992. if (ret < 0) {
  1993. mlog_errno(ret);
  1994. goto out;
  1995. }
  1996. if (direct_io && !is_sync_kiocb(iocb))
  1997. unaligned_dio = ocfs2_is_io_unaligned(inode, count, iocb->ki_pos);
  1998. /*
  1999. * We can't complete the direct I/O as requested, fall back to
  2000. * buffered I/O.
  2001. */
  2002. if (direct_io && !can_do_direct) {
  2003. ocfs2_rw_unlock(inode, rw_level);
  2004. have_alloc_sem = 0;
  2005. rw_level = -1;
  2006. direct_io = 0;
  2007. iocb->ki_flags &= ~IOCB_DIRECT;
  2008. iov_iter_reexpand(from, orig_count);
  2009. dropped_dio = 1;
  2010. goto relock;
  2011. }
  2012. if (unaligned_dio) {
  2013. /*
  2014. * Wait on previous unaligned aio to complete before
  2015. * proceeding.
  2016. */
  2017. mutex_lock(&OCFS2_I(inode)->ip_unaligned_aio);
  2018. /* Mark the iocb as needing an unlock in ocfs2_dio_end_io */
  2019. ocfs2_iocb_set_unaligned_aio(iocb);
  2020. }
  2021. /*
  2022. * To later detect whether a journal commit for sync writes is
  2023. * necessary, we sample i_size, and cluster count here.
  2024. */
  2025. old_size = i_size_read(inode);
  2026. old_clusters = OCFS2_I(inode)->ip_clusters;
  2027. /* communicate with ocfs2_dio_end_io */
  2028. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2029. written = __generic_file_write_iter(iocb, from);
  2030. /* buffered aio wouldn't have proper lock coverage today */
  2031. BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
  2032. if (unlikely(written <= 0))
  2033. goto no_sync;
  2034. if (((file->f_flags & O_DSYNC) && !direct_io) ||
  2035. IS_SYNC(inode) || dropped_dio) {
  2036. ret = filemap_fdatawrite_range(file->f_mapping,
  2037. iocb->ki_pos - written,
  2038. iocb->ki_pos - 1);
  2039. if (ret < 0)
  2040. written = ret;
  2041. if (!ret) {
  2042. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  2043. if (ret < 0)
  2044. written = ret;
  2045. }
  2046. if (!ret)
  2047. ret = filemap_fdatawait_range(file->f_mapping,
  2048. iocb->ki_pos - written,
  2049. iocb->ki_pos - 1);
  2050. }
  2051. no_sync:
  2052. /*
  2053. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  2054. * function pointer which is called when o_direct io completes so that
  2055. * it can unlock our rw lock.
  2056. * Unfortunately there are error cases which call end_io and others
  2057. * that don't. so we don't have to unlock the rw_lock if either an
  2058. * async dio is going to do it in the future or an end_io after an
  2059. * error has already done it.
  2060. */
  2061. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  2062. rw_level = -1;
  2063. have_alloc_sem = 0;
  2064. unaligned_dio = 0;
  2065. }
  2066. if (unaligned_dio) {
  2067. ocfs2_iocb_clear_unaligned_aio(iocb);
  2068. mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio);
  2069. }
  2070. out:
  2071. if (rw_level != -1)
  2072. ocfs2_rw_unlock(inode, rw_level);
  2073. out_sems:
  2074. if (have_alloc_sem)
  2075. ocfs2_iocb_clear_sem_locked(iocb);
  2076. mutex_unlock(&inode->i_mutex);
  2077. if (written)
  2078. ret = written;
  2079. return ret;
  2080. }
  2081. static ssize_t ocfs2_file_splice_read(struct file *in,
  2082. loff_t *ppos,
  2083. struct pipe_inode_info *pipe,
  2084. size_t len,
  2085. unsigned int flags)
  2086. {
  2087. int ret = 0, lock_level = 0;
  2088. struct inode *inode = file_inode(in);
  2089. trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
  2090. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2091. in->f_path.dentry->d_name.len,
  2092. in->f_path.dentry->d_name.name, len);
  2093. /*
  2094. * See the comment in ocfs2_file_read_iter()
  2095. */
  2096. ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level);
  2097. if (ret < 0) {
  2098. mlog_errno(ret);
  2099. goto bail;
  2100. }
  2101. ocfs2_inode_unlock(inode, lock_level);
  2102. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  2103. bail:
  2104. return ret;
  2105. }
  2106. static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
  2107. struct iov_iter *to)
  2108. {
  2109. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  2110. struct file *filp = iocb->ki_filp;
  2111. struct inode *inode = file_inode(filp);
  2112. trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
  2113. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2114. filp->f_path.dentry->d_name.len,
  2115. filp->f_path.dentry->d_name.name,
  2116. to->nr_segs); /* GRRRRR */
  2117. if (!inode) {
  2118. ret = -EINVAL;
  2119. mlog_errno(ret);
  2120. goto bail;
  2121. }
  2122. ocfs2_iocb_clear_sem_locked(iocb);
  2123. /*
  2124. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  2125. * need locks to protect pending reads from racing with truncate.
  2126. */
  2127. if (iocb->ki_flags & IOCB_DIRECT) {
  2128. have_alloc_sem = 1;
  2129. ocfs2_iocb_set_sem_locked(iocb);
  2130. ret = ocfs2_rw_lock(inode, 0);
  2131. if (ret < 0) {
  2132. mlog_errno(ret);
  2133. goto bail;
  2134. }
  2135. rw_level = 0;
  2136. /* communicate with ocfs2_dio_end_io */
  2137. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2138. }
  2139. /*
  2140. * We're fine letting folks race truncates and extending
  2141. * writes with read across the cluster, just like they can
  2142. * locally. Hence no rw_lock during read.
  2143. *
  2144. * Take and drop the meta data lock to update inode fields
  2145. * like i_size. This allows the checks down below
  2146. * generic_file_aio_read() a chance of actually working.
  2147. */
  2148. ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level);
  2149. if (ret < 0) {
  2150. mlog_errno(ret);
  2151. goto bail;
  2152. }
  2153. ocfs2_inode_unlock(inode, lock_level);
  2154. ret = generic_file_read_iter(iocb, to);
  2155. trace_generic_file_aio_read_ret(ret);
  2156. /* buffered aio wouldn't have proper lock coverage today */
  2157. BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
  2158. /* see ocfs2_file_write_iter */
  2159. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2160. rw_level = -1;
  2161. have_alloc_sem = 0;
  2162. }
  2163. bail:
  2164. if (have_alloc_sem)
  2165. ocfs2_iocb_clear_sem_locked(iocb);
  2166. if (rw_level != -1)
  2167. ocfs2_rw_unlock(inode, rw_level);
  2168. return ret;
  2169. }
  2170. /* Refer generic_file_llseek_unlocked() */
  2171. static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
  2172. {
  2173. struct inode *inode = file->f_mapping->host;
  2174. int ret = 0;
  2175. mutex_lock(&inode->i_mutex);
  2176. switch (whence) {
  2177. case SEEK_SET:
  2178. break;
  2179. case SEEK_END:
  2180. /* SEEK_END requires the OCFS2 inode lock for the file
  2181. * because it references the file's size.
  2182. */
  2183. ret = ocfs2_inode_lock(inode, NULL, 0);
  2184. if (ret < 0) {
  2185. mlog_errno(ret);
  2186. goto out;
  2187. }
  2188. offset += i_size_read(inode);
  2189. ocfs2_inode_unlock(inode, 0);
  2190. break;
  2191. case SEEK_CUR:
  2192. if (offset == 0) {
  2193. offset = file->f_pos;
  2194. goto out;
  2195. }
  2196. offset += file->f_pos;
  2197. break;
  2198. case SEEK_DATA:
  2199. case SEEK_HOLE:
  2200. ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
  2201. if (ret)
  2202. goto out;
  2203. break;
  2204. default:
  2205. ret = -EINVAL;
  2206. goto out;
  2207. }
  2208. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  2209. out:
  2210. mutex_unlock(&inode->i_mutex);
  2211. if (ret)
  2212. return ret;
  2213. return offset;
  2214. }
  2215. const struct inode_operations ocfs2_file_iops = {
  2216. .setattr = ocfs2_setattr,
  2217. .getattr = ocfs2_getattr,
  2218. .permission = ocfs2_permission,
  2219. .setxattr = generic_setxattr,
  2220. .getxattr = generic_getxattr,
  2221. .listxattr = ocfs2_listxattr,
  2222. .removexattr = generic_removexattr,
  2223. .fiemap = ocfs2_fiemap,
  2224. .get_acl = ocfs2_iop_get_acl,
  2225. .set_acl = ocfs2_iop_set_acl,
  2226. };
  2227. const struct inode_operations ocfs2_special_file_iops = {
  2228. .setattr = ocfs2_setattr,
  2229. .getattr = ocfs2_getattr,
  2230. .permission = ocfs2_permission,
  2231. .get_acl = ocfs2_iop_get_acl,
  2232. .set_acl = ocfs2_iop_set_acl,
  2233. };
  2234. /*
  2235. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2236. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2237. */
  2238. const struct file_operations ocfs2_fops = {
  2239. .llseek = ocfs2_file_llseek,
  2240. .mmap = ocfs2_mmap,
  2241. .fsync = ocfs2_sync_file,
  2242. .release = ocfs2_file_release,
  2243. .open = ocfs2_file_open,
  2244. .read_iter = ocfs2_file_read_iter,
  2245. .write_iter = ocfs2_file_write_iter,
  2246. .unlocked_ioctl = ocfs2_ioctl,
  2247. #ifdef CONFIG_COMPAT
  2248. .compat_ioctl = ocfs2_compat_ioctl,
  2249. #endif
  2250. .lock = ocfs2_lock,
  2251. .flock = ocfs2_flock,
  2252. .splice_read = ocfs2_file_splice_read,
  2253. .splice_write = iter_file_splice_write,
  2254. .fallocate = ocfs2_fallocate,
  2255. };
  2256. const struct file_operations ocfs2_dops = {
  2257. .llseek = generic_file_llseek,
  2258. .read = generic_read_dir,
  2259. .iterate = ocfs2_readdir,
  2260. .fsync = ocfs2_sync_file,
  2261. .release = ocfs2_dir_release,
  2262. .open = ocfs2_dir_open,
  2263. .unlocked_ioctl = ocfs2_ioctl,
  2264. #ifdef CONFIG_COMPAT
  2265. .compat_ioctl = ocfs2_compat_ioctl,
  2266. #endif
  2267. .lock = ocfs2_lock,
  2268. .flock = ocfs2_flock,
  2269. };
  2270. /*
  2271. * POSIX-lockless variants of our file_operations.
  2272. *
  2273. * These will be used if the underlying cluster stack does not support
  2274. * posix file locking, if the user passes the "localflocks" mount
  2275. * option, or if we have a local-only fs.
  2276. *
  2277. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2278. * so we still want it in the case of no stack support for
  2279. * plocks. Internally, it will do the right thing when asked to ignore
  2280. * the cluster.
  2281. */
  2282. const struct file_operations ocfs2_fops_no_plocks = {
  2283. .llseek = ocfs2_file_llseek,
  2284. .mmap = ocfs2_mmap,
  2285. .fsync = ocfs2_sync_file,
  2286. .release = ocfs2_file_release,
  2287. .open = ocfs2_file_open,
  2288. .read_iter = ocfs2_file_read_iter,
  2289. .write_iter = ocfs2_file_write_iter,
  2290. .unlocked_ioctl = ocfs2_ioctl,
  2291. #ifdef CONFIG_COMPAT
  2292. .compat_ioctl = ocfs2_compat_ioctl,
  2293. #endif
  2294. .flock = ocfs2_flock,
  2295. .splice_read = ocfs2_file_splice_read,
  2296. .splice_write = iter_file_splice_write,
  2297. .fallocate = ocfs2_fallocate,
  2298. };
  2299. const struct file_operations ocfs2_dops_no_plocks = {
  2300. .llseek = generic_file_llseek,
  2301. .read = generic_read_dir,
  2302. .iterate = ocfs2_readdir,
  2303. .fsync = ocfs2_sync_file,
  2304. .release = ocfs2_dir_release,
  2305. .open = ocfs2_dir_open,
  2306. .unlocked_ioctl = ocfs2_ioctl,
  2307. #ifdef CONFIG_COMPAT
  2308. .compat_ioctl = ocfs2_compat_ioctl,
  2309. #endif
  2310. .flock = ocfs2_flock,
  2311. };