file.c 65 KB

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