file.c 67 KB

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