file.c 71 KB

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