file.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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. journal_t *journal;
  150. struct inode *inode = file->f_mapping->host;
  151. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  152. trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
  153. OCFS2_I(inode)->ip_blkno,
  154. file->f_path.dentry->d_name.len,
  155. file->f_path.dentry->d_name.name,
  156. (unsigned long long)datasync);
  157. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  158. if (err)
  159. return err;
  160. /*
  161. * Probably don't need the i_mutex at all in here, just putting it here
  162. * to be consistent with how fsync used to be called, someone more
  163. * familiar with the fs could possibly remove it.
  164. */
  165. mutex_lock(&inode->i_mutex);
  166. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) {
  167. /*
  168. * We still have to flush drive's caches to get data to the
  169. * platter
  170. */
  171. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  172. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  173. goto bail;
  174. }
  175. journal = osb->journal->j_journal;
  176. err = jbd2_journal_force_commit(journal);
  177. bail:
  178. if (err)
  179. mlog_errno(err);
  180. mutex_unlock(&inode->i_mutex);
  181. return (err < 0) ? -EIO : 0;
  182. }
  183. int ocfs2_should_update_atime(struct inode *inode,
  184. struct vfsmount *vfsmnt)
  185. {
  186. struct timespec now;
  187. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  188. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  189. return 0;
  190. if ((inode->i_flags & S_NOATIME) ||
  191. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  192. return 0;
  193. /*
  194. * We can be called with no vfsmnt structure - NFSD will
  195. * sometimes do this.
  196. *
  197. * Note that our action here is different than touch_atime() -
  198. * if we can't tell whether this is a noatime mount, then we
  199. * don't know whether to trust the value of s_atime_quantum.
  200. */
  201. if (vfsmnt == NULL)
  202. return 0;
  203. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  204. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  205. return 0;
  206. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  207. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  208. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  209. return 1;
  210. return 0;
  211. }
  212. now = CURRENT_TIME;
  213. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  214. return 0;
  215. else
  216. return 1;
  217. }
  218. int ocfs2_update_inode_atime(struct inode *inode,
  219. struct buffer_head *bh)
  220. {
  221. int ret;
  222. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  223. handle_t *handle;
  224. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  225. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  226. if (IS_ERR(handle)) {
  227. ret = PTR_ERR(handle);
  228. mlog_errno(ret);
  229. goto out;
  230. }
  231. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  232. OCFS2_JOURNAL_ACCESS_WRITE);
  233. if (ret) {
  234. mlog_errno(ret);
  235. goto out_commit;
  236. }
  237. /*
  238. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  239. * have i_mutex to guard against concurrent changes to other
  240. * inode fields.
  241. */
  242. inode->i_atime = CURRENT_TIME;
  243. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  244. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  245. ocfs2_journal_dirty(handle, bh);
  246. out_commit:
  247. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  248. out:
  249. return ret;
  250. }
  251. static int ocfs2_set_inode_size(handle_t *handle,
  252. struct inode *inode,
  253. struct buffer_head *fe_bh,
  254. u64 new_i_size)
  255. {
  256. int status;
  257. i_size_write(inode, new_i_size);
  258. inode->i_blocks = ocfs2_inode_sector_count(inode);
  259. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  260. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  261. if (status < 0) {
  262. mlog_errno(status);
  263. goto bail;
  264. }
  265. bail:
  266. return status;
  267. }
  268. int ocfs2_simple_size_update(struct inode *inode,
  269. struct buffer_head *di_bh,
  270. u64 new_i_size)
  271. {
  272. int ret;
  273. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  274. handle_t *handle = NULL;
  275. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  276. if (IS_ERR(handle)) {
  277. ret = PTR_ERR(handle);
  278. mlog_errno(ret);
  279. goto out;
  280. }
  281. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  282. new_i_size);
  283. if (ret < 0)
  284. mlog_errno(ret);
  285. ocfs2_commit_trans(osb, handle);
  286. out:
  287. return ret;
  288. }
  289. static int ocfs2_cow_file_pos(struct inode *inode,
  290. struct buffer_head *fe_bh,
  291. u64 offset)
  292. {
  293. int status;
  294. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  295. unsigned int num_clusters = 0;
  296. unsigned int ext_flags = 0;
  297. /*
  298. * If the new offset is aligned to the range of the cluster, there is
  299. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  300. * CoW either.
  301. */
  302. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  303. return 0;
  304. status = ocfs2_get_clusters(inode, cpos, &phys,
  305. &num_clusters, &ext_flags);
  306. if (status) {
  307. mlog_errno(status);
  308. goto out;
  309. }
  310. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  311. goto out;
  312. return ocfs2_refcount_cow(inode, NULL, fe_bh, cpos, 1, cpos+1);
  313. out:
  314. return status;
  315. }
  316. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  317. struct inode *inode,
  318. struct buffer_head *fe_bh,
  319. u64 new_i_size)
  320. {
  321. int status;
  322. handle_t *handle;
  323. struct ocfs2_dinode *di;
  324. u64 cluster_bytes;
  325. /*
  326. * We need to CoW the cluster contains the offset if it is reflinked
  327. * since we will call ocfs2_zero_range_for_truncate later which will
  328. * write "0" from offset to the end of the cluster.
  329. */
  330. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  331. if (status) {
  332. mlog_errno(status);
  333. return status;
  334. }
  335. /* TODO: This needs to actually orphan the inode in this
  336. * transaction. */
  337. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  338. if (IS_ERR(handle)) {
  339. status = PTR_ERR(handle);
  340. mlog_errno(status);
  341. goto out;
  342. }
  343. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  344. OCFS2_JOURNAL_ACCESS_WRITE);
  345. if (status < 0) {
  346. mlog_errno(status);
  347. goto out_commit;
  348. }
  349. /*
  350. * Do this before setting i_size.
  351. */
  352. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  353. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  354. cluster_bytes);
  355. if (status) {
  356. mlog_errno(status);
  357. goto out_commit;
  358. }
  359. i_size_write(inode, new_i_size);
  360. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  361. di = (struct ocfs2_dinode *) fe_bh->b_data;
  362. di->i_size = cpu_to_le64(new_i_size);
  363. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  364. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  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. /* lets handle the simple truncate cases before doing any more
  400. * cluster locking. */
  401. if (new_i_size == le64_to_cpu(fe->i_size))
  402. goto bail;
  403. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  404. ocfs2_resv_discard(&osb->osb_la_resmap,
  405. &OCFS2_I(inode)->ip_la_data_resv);
  406. /*
  407. * The inode lock forced other nodes to sync and drop their
  408. * pages, which (correctly) happens even if we have a truncate
  409. * without allocation change - ocfs2 cluster sizes can be much
  410. * greater than page size, so we have to truncate them
  411. * anyway.
  412. */
  413. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  414. truncate_inode_pages(inode->i_mapping, new_i_size);
  415. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  416. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  417. i_size_read(inode), 1);
  418. if (status)
  419. mlog_errno(status);
  420. goto bail_unlock_sem;
  421. }
  422. /* alright, we're going to need to do a full blown alloc size
  423. * change. Orphan the inode so that recovery can complete the
  424. * truncate if necessary. This does the task of marking
  425. * i_size. */
  426. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  427. if (status < 0) {
  428. mlog_errno(status);
  429. goto bail_unlock_sem;
  430. }
  431. status = ocfs2_commit_truncate(osb, inode, di_bh);
  432. if (status < 0) {
  433. mlog_errno(status);
  434. goto bail_unlock_sem;
  435. }
  436. /* TODO: orphan dir cleanup here. */
  437. bail_unlock_sem:
  438. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  439. bail:
  440. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  441. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  442. return status;
  443. }
  444. /*
  445. * extend file allocation only here.
  446. * we'll update all the disk stuff, and oip->alloc_size
  447. *
  448. * expect stuff to be locked, a transaction started and enough data /
  449. * metadata reservations in the contexts.
  450. *
  451. * Will return -EAGAIN, and a reason if a restart is needed.
  452. * If passed in, *reason will always be set, even in error.
  453. */
  454. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  455. struct inode *inode,
  456. u32 *logical_offset,
  457. u32 clusters_to_add,
  458. int mark_unwritten,
  459. struct buffer_head *fe_bh,
  460. handle_t *handle,
  461. struct ocfs2_alloc_context *data_ac,
  462. struct ocfs2_alloc_context *meta_ac,
  463. enum ocfs2_alloc_restarted *reason_ret)
  464. {
  465. int ret;
  466. struct ocfs2_extent_tree et;
  467. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
  468. ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
  469. clusters_to_add, mark_unwritten,
  470. data_ac, meta_ac, reason_ret);
  471. return ret;
  472. }
  473. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  474. u32 clusters_to_add, int mark_unwritten)
  475. {
  476. int status = 0;
  477. int restart_func = 0;
  478. int credits;
  479. u32 prev_clusters;
  480. struct buffer_head *bh = NULL;
  481. struct ocfs2_dinode *fe = NULL;
  482. handle_t *handle = NULL;
  483. struct ocfs2_alloc_context *data_ac = NULL;
  484. struct ocfs2_alloc_context *meta_ac = NULL;
  485. enum ocfs2_alloc_restarted why;
  486. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  487. struct ocfs2_extent_tree et;
  488. int did_quota = 0;
  489. /*
  490. * This function only exists for file systems which don't
  491. * support holes.
  492. */
  493. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  494. status = ocfs2_read_inode_block(inode, &bh);
  495. if (status < 0) {
  496. mlog_errno(status);
  497. goto leave;
  498. }
  499. fe = (struct ocfs2_dinode *) bh->b_data;
  500. restart_all:
  501. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  502. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  503. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  504. &data_ac, &meta_ac);
  505. if (status) {
  506. mlog_errno(status);
  507. goto leave;
  508. }
  509. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  510. clusters_to_add);
  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_journal_dirty(handle, bh);
  555. spin_lock(&OCFS2_I(inode)->ip_lock);
  556. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  557. spin_unlock(&OCFS2_I(inode)->ip_lock);
  558. /* Release unused quota reservation */
  559. dquot_free_space(inode,
  560. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  561. did_quota = 0;
  562. if (why != RESTART_NONE && clusters_to_add) {
  563. if (why == RESTART_META) {
  564. restart_func = 1;
  565. status = 0;
  566. } else {
  567. BUG_ON(why != RESTART_TRANS);
  568. /* TODO: This can be more intelligent. */
  569. credits = ocfs2_calc_extend_credits(osb->sb,
  570. &fe->id2.i_list,
  571. clusters_to_add);
  572. status = ocfs2_extend_trans(handle, credits);
  573. if (status < 0) {
  574. /* handle still has to be committed at
  575. * this point. */
  576. status = -ENOMEM;
  577. mlog_errno(status);
  578. goto leave;
  579. }
  580. goto restarted_transaction;
  581. }
  582. }
  583. trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
  584. le32_to_cpu(fe->i_clusters),
  585. (unsigned long long)le64_to_cpu(fe->i_size),
  586. OCFS2_I(inode)->ip_clusters,
  587. (unsigned long long)i_size_read(inode));
  588. leave:
  589. if (status < 0 && did_quota)
  590. dquot_free_space(inode,
  591. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  592. if (handle) {
  593. ocfs2_commit_trans(osb, handle);
  594. handle = NULL;
  595. }
  596. if (data_ac) {
  597. ocfs2_free_alloc_context(data_ac);
  598. data_ac = NULL;
  599. }
  600. if (meta_ac) {
  601. ocfs2_free_alloc_context(meta_ac);
  602. meta_ac = NULL;
  603. }
  604. if ((!status) && restart_func) {
  605. restart_func = 0;
  606. goto restart_all;
  607. }
  608. brelse(bh);
  609. bh = NULL;
  610. return status;
  611. }
  612. /*
  613. * While a write will already be ordering the data, a truncate will not.
  614. * Thus, we need to explicitly order the zeroed pages.
  615. */
  616. static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
  617. {
  618. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  619. handle_t *handle = NULL;
  620. int ret = 0;
  621. if (!ocfs2_should_order_data(inode))
  622. goto out;
  623. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  624. if (IS_ERR(handle)) {
  625. ret = -ENOMEM;
  626. mlog_errno(ret);
  627. goto out;
  628. }
  629. ret = ocfs2_jbd2_file_inode(handle, inode);
  630. if (ret < 0)
  631. mlog_errno(ret);
  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)
  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. BUG_ON(abs_from >= abs_to);
  653. BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
  654. BUG_ON(abs_from & (inode->i_blkbits - 1));
  655. page = find_or_create_page(mapping, index, GFP_NOFS);
  656. if (!page) {
  657. ret = -ENOMEM;
  658. mlog_errno(ret);
  659. goto out;
  660. }
  661. /* Get the offsets within the page that we want to zero */
  662. zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
  663. zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
  664. if (!zero_to)
  665. zero_to = PAGE_CACHE_SIZE;
  666. trace_ocfs2_write_zero_page(
  667. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  668. (unsigned long long)abs_from,
  669. (unsigned long long)abs_to,
  670. index, zero_from, zero_to);
  671. /* We know that zero_from is block aligned */
  672. for (block_start = zero_from; block_start < zero_to;
  673. block_start = block_end) {
  674. block_end = block_start + (1 << inode->i_blkbits);
  675. /*
  676. * block_start is block-aligned. Bump it by one to force
  677. * __block_write_begin and block_commit_write to zero the
  678. * whole block.
  679. */
  680. ret = __block_write_begin(page, block_start + 1, 0,
  681. ocfs2_get_block);
  682. if (ret < 0) {
  683. mlog_errno(ret);
  684. goto out_unlock;
  685. }
  686. if (!handle) {
  687. handle = ocfs2_zero_start_ordered_transaction(inode);
  688. if (IS_ERR(handle)) {
  689. ret = PTR_ERR(handle);
  690. handle = NULL;
  691. break;
  692. }
  693. }
  694. /* must not update i_size! */
  695. ret = block_commit_write(page, block_start + 1,
  696. block_start + 1);
  697. if (ret < 0)
  698. mlog_errno(ret);
  699. else
  700. ret = 0;
  701. }
  702. if (handle)
  703. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  704. out_unlock:
  705. unlock_page(page);
  706. page_cache_release(page);
  707. out:
  708. return ret;
  709. }
  710. /*
  711. * Find the next range to zero. We do this in terms of bytes because
  712. * that's what ocfs2_zero_extend() wants, and it is dealing with the
  713. * pagecache. We may return multiple extents.
  714. *
  715. * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
  716. * needs to be zeroed. range_start and range_end return the next zeroing
  717. * range. A subsequent call should pass the previous range_end as its
  718. * zero_start. If range_end is 0, there's nothing to do.
  719. *
  720. * Unwritten extents are skipped over. Refcounted extents are CoWd.
  721. */
  722. static int ocfs2_zero_extend_get_range(struct inode *inode,
  723. struct buffer_head *di_bh,
  724. u64 zero_start, u64 zero_end,
  725. u64 *range_start, u64 *range_end)
  726. {
  727. int rc = 0, needs_cow = 0;
  728. u32 p_cpos, zero_clusters = 0;
  729. u32 zero_cpos =
  730. zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  731. u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
  732. unsigned int num_clusters = 0;
  733. unsigned int ext_flags = 0;
  734. while (zero_cpos < last_cpos) {
  735. rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
  736. &num_clusters, &ext_flags);
  737. if (rc) {
  738. mlog_errno(rc);
  739. goto out;
  740. }
  741. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  742. zero_clusters = num_clusters;
  743. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  744. needs_cow = 1;
  745. break;
  746. }
  747. zero_cpos += num_clusters;
  748. }
  749. if (!zero_clusters) {
  750. *range_end = 0;
  751. goto out;
  752. }
  753. while ((zero_cpos + zero_clusters) < last_cpos) {
  754. rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
  755. &p_cpos, &num_clusters,
  756. &ext_flags);
  757. if (rc) {
  758. mlog_errno(rc);
  759. goto out;
  760. }
  761. if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
  762. break;
  763. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  764. needs_cow = 1;
  765. zero_clusters += num_clusters;
  766. }
  767. if ((zero_cpos + zero_clusters) > last_cpos)
  768. zero_clusters = last_cpos - zero_cpos;
  769. if (needs_cow) {
  770. rc = ocfs2_refcount_cow(inode, NULL, di_bh, zero_cpos,
  771. zero_clusters, UINT_MAX);
  772. if (rc) {
  773. mlog_errno(rc);
  774. goto out;
  775. }
  776. }
  777. *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
  778. *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
  779. zero_cpos + zero_clusters);
  780. out:
  781. return rc;
  782. }
  783. /*
  784. * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
  785. * has made sure that the entire range needs zeroing.
  786. */
  787. static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
  788. u64 range_end)
  789. {
  790. int rc = 0;
  791. u64 next_pos;
  792. u64 zero_pos = range_start;
  793. trace_ocfs2_zero_extend_range(
  794. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  795. (unsigned long long)range_start,
  796. (unsigned long long)range_end);
  797. BUG_ON(range_start >= range_end);
  798. while (zero_pos < range_end) {
  799. next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
  800. if (next_pos > range_end)
  801. next_pos = range_end;
  802. rc = ocfs2_write_zero_page(inode, zero_pos, next_pos);
  803. if (rc < 0) {
  804. mlog_errno(rc);
  805. break;
  806. }
  807. zero_pos = next_pos;
  808. /*
  809. * Very large extends have the potential to lock up
  810. * the cpu for extended periods of time.
  811. */
  812. cond_resched();
  813. }
  814. return rc;
  815. }
  816. int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
  817. loff_t zero_to_size)
  818. {
  819. int ret = 0;
  820. u64 zero_start, range_start = 0, range_end = 0;
  821. struct super_block *sb = inode->i_sb;
  822. zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  823. trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
  824. (unsigned long long)zero_start,
  825. (unsigned long long)i_size_read(inode));
  826. while (zero_start < zero_to_size) {
  827. ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
  828. zero_to_size,
  829. &range_start,
  830. &range_end);
  831. if (ret) {
  832. mlog_errno(ret);
  833. break;
  834. }
  835. if (!range_end)
  836. break;
  837. /* Trim the ends */
  838. if (range_start < zero_start)
  839. range_start = zero_start;
  840. if (range_end > zero_to_size)
  841. range_end = zero_to_size;
  842. ret = ocfs2_zero_extend_range(inode, range_start,
  843. range_end);
  844. if (ret) {
  845. mlog_errno(ret);
  846. break;
  847. }
  848. zero_start = range_end;
  849. }
  850. return ret;
  851. }
  852. int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
  853. u64 new_i_size, u64 zero_to)
  854. {
  855. int ret;
  856. u32 clusters_to_add;
  857. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  858. /*
  859. * Only quota files call this without a bh, and they can't be
  860. * refcounted.
  861. */
  862. BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  863. BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
  864. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  865. if (clusters_to_add < oi->ip_clusters)
  866. clusters_to_add = 0;
  867. else
  868. clusters_to_add -= oi->ip_clusters;
  869. if (clusters_to_add) {
  870. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  871. clusters_to_add, 0);
  872. if (ret) {
  873. mlog_errno(ret);
  874. goto out;
  875. }
  876. }
  877. /*
  878. * Call this even if we don't add any clusters to the tree. We
  879. * still need to zero the area between the old i_size and the
  880. * new i_size.
  881. */
  882. ret = ocfs2_zero_extend(inode, di_bh, zero_to);
  883. if (ret < 0)
  884. mlog_errno(ret);
  885. out:
  886. return ret;
  887. }
  888. static int ocfs2_extend_file(struct inode *inode,
  889. struct buffer_head *di_bh,
  890. u64 new_i_size)
  891. {
  892. int ret = 0;
  893. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  894. BUG_ON(!di_bh);
  895. /* setattr sometimes calls us like this. */
  896. if (new_i_size == 0)
  897. goto out;
  898. if (i_size_read(inode) == new_i_size)
  899. goto out;
  900. BUG_ON(new_i_size < i_size_read(inode));
  901. /*
  902. * The alloc sem blocks people in read/write from reading our
  903. * allocation until we're done changing it. We depend on
  904. * i_mutex to block other extend/truncate calls while we're
  905. * here. We even have to hold it for sparse files because there
  906. * might be some tail zeroing.
  907. */
  908. down_write(&oi->ip_alloc_sem);
  909. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  910. /*
  911. * We can optimize small extends by keeping the inodes
  912. * inline data.
  913. */
  914. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  915. up_write(&oi->ip_alloc_sem);
  916. goto out_update_size;
  917. }
  918. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  919. if (ret) {
  920. up_write(&oi->ip_alloc_sem);
  921. mlog_errno(ret);
  922. goto out;
  923. }
  924. }
  925. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  926. ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
  927. else
  928. ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
  929. new_i_size);
  930. up_write(&oi->ip_alloc_sem);
  931. if (ret < 0) {
  932. mlog_errno(ret);
  933. goto out;
  934. }
  935. out_update_size:
  936. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  937. if (ret < 0)
  938. mlog_errno(ret);
  939. out:
  940. return ret;
  941. }
  942. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  943. {
  944. int status = 0, size_change;
  945. struct inode *inode = dentry->d_inode;
  946. struct super_block *sb = inode->i_sb;
  947. struct ocfs2_super *osb = OCFS2_SB(sb);
  948. struct buffer_head *bh = NULL;
  949. handle_t *handle = NULL;
  950. struct dquot *transfer_to[MAXQUOTAS] = { };
  951. int qtype;
  952. trace_ocfs2_setattr(inode, dentry,
  953. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  954. dentry->d_name.len, dentry->d_name.name,
  955. attr->ia_valid, attr->ia_mode,
  956. attr->ia_uid, attr->ia_gid);
  957. /* ensuring we don't even attempt to truncate a symlink */
  958. if (S_ISLNK(inode->i_mode))
  959. attr->ia_valid &= ~ATTR_SIZE;
  960. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  961. | ATTR_GID | ATTR_UID | ATTR_MODE)
  962. if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
  963. return 0;
  964. status = inode_change_ok(inode, attr);
  965. if (status)
  966. return status;
  967. if (is_quota_modification(inode, attr))
  968. dquot_initialize(inode);
  969. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  970. if (size_change) {
  971. status = ocfs2_rw_lock(inode, 1);
  972. if (status < 0) {
  973. mlog_errno(status);
  974. goto bail;
  975. }
  976. }
  977. status = ocfs2_inode_lock(inode, &bh, 1);
  978. if (status < 0) {
  979. if (status != -ENOENT)
  980. mlog_errno(status);
  981. goto bail_unlock_rw;
  982. }
  983. if (size_change && attr->ia_size != i_size_read(inode)) {
  984. status = inode_newsize_ok(inode, attr->ia_size);
  985. if (status)
  986. goto bail_unlock;
  987. inode_dio_wait(inode);
  988. if (i_size_read(inode) > attr->ia_size) {
  989. if (ocfs2_should_order_data(inode)) {
  990. status = ocfs2_begin_ordered_truncate(inode,
  991. attr->ia_size);
  992. if (status)
  993. goto bail_unlock;
  994. }
  995. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  996. } else
  997. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  998. if (status < 0) {
  999. if (status != -ENOSPC)
  1000. mlog_errno(status);
  1001. status = -ENOSPC;
  1002. goto bail_unlock;
  1003. }
  1004. }
  1005. if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  1006. (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  1007. /*
  1008. * Gather pointers to quota structures so that allocation /
  1009. * freeing of quota structures happens here and not inside
  1010. * dquot_transfer() where we have problems with lock ordering
  1011. */
  1012. if (attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid
  1013. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1014. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1015. transfer_to[USRQUOTA] = dqget(sb, attr->ia_uid,
  1016. USRQUOTA);
  1017. if (!transfer_to[USRQUOTA]) {
  1018. status = -ESRCH;
  1019. goto bail_unlock;
  1020. }
  1021. }
  1022. if (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid
  1023. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1024. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1025. transfer_to[GRPQUOTA] = dqget(sb, attr->ia_gid,
  1026. GRPQUOTA);
  1027. if (!transfer_to[GRPQUOTA]) {
  1028. status = -ESRCH;
  1029. goto bail_unlock;
  1030. }
  1031. }
  1032. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  1033. 2 * ocfs2_quota_trans_credits(sb));
  1034. if (IS_ERR(handle)) {
  1035. status = PTR_ERR(handle);
  1036. mlog_errno(status);
  1037. goto bail_unlock;
  1038. }
  1039. status = __dquot_transfer(inode, transfer_to);
  1040. if (status < 0)
  1041. goto bail_commit;
  1042. } else {
  1043. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1044. if (IS_ERR(handle)) {
  1045. status = PTR_ERR(handle);
  1046. mlog_errno(status);
  1047. goto bail_unlock;
  1048. }
  1049. }
  1050. /*
  1051. * This will intentionally not wind up calling truncate_setsize(),
  1052. * since all the work for a size change has been done above.
  1053. * Otherwise, we could get into problems with truncate as
  1054. * ip_alloc_sem is used there to protect against i_size
  1055. * changes.
  1056. *
  1057. * XXX: this means the conditional below can probably be removed.
  1058. */
  1059. if ((attr->ia_valid & ATTR_SIZE) &&
  1060. attr->ia_size != i_size_read(inode)) {
  1061. status = vmtruncate(inode, attr->ia_size);
  1062. if (status) {
  1063. mlog_errno(status);
  1064. goto bail_commit;
  1065. }
  1066. }
  1067. setattr_copy(inode, attr);
  1068. mark_inode_dirty(inode);
  1069. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  1070. if (status < 0)
  1071. mlog_errno(status);
  1072. bail_commit:
  1073. ocfs2_commit_trans(osb, handle);
  1074. bail_unlock:
  1075. ocfs2_inode_unlock(inode, 1);
  1076. bail_unlock_rw:
  1077. if (size_change)
  1078. ocfs2_rw_unlock(inode, 1);
  1079. bail:
  1080. brelse(bh);
  1081. /* Release quota pointers in case we acquired them */
  1082. for (qtype = 0; qtype < MAXQUOTAS; qtype++)
  1083. dqput(transfer_to[qtype]);
  1084. if (!status && attr->ia_valid & ATTR_MODE) {
  1085. status = ocfs2_acl_chmod(inode);
  1086. if (status < 0)
  1087. mlog_errno(status);
  1088. }
  1089. return status;
  1090. }
  1091. int ocfs2_getattr(struct vfsmount *mnt,
  1092. struct dentry *dentry,
  1093. struct kstat *stat)
  1094. {
  1095. struct inode *inode = dentry->d_inode;
  1096. struct super_block *sb = dentry->d_inode->i_sb;
  1097. struct ocfs2_super *osb = sb->s_fs_info;
  1098. int err;
  1099. err = ocfs2_inode_revalidate(dentry);
  1100. if (err) {
  1101. if (err != -ENOENT)
  1102. mlog_errno(err);
  1103. goto bail;
  1104. }
  1105. generic_fillattr(inode, stat);
  1106. /* We set the blksize from the cluster size for performance */
  1107. stat->blksize = osb->s_clustersize;
  1108. bail:
  1109. return err;
  1110. }
  1111. int ocfs2_permission(struct inode *inode, int mask)
  1112. {
  1113. int ret;
  1114. if (mask & MAY_NOT_BLOCK)
  1115. return -ECHILD;
  1116. ret = ocfs2_inode_lock(inode, NULL, 0);
  1117. if (ret) {
  1118. if (ret != -ENOENT)
  1119. mlog_errno(ret);
  1120. goto out;
  1121. }
  1122. ret = generic_permission(inode, mask);
  1123. ocfs2_inode_unlock(inode, 0);
  1124. out:
  1125. return ret;
  1126. }
  1127. static int __ocfs2_write_remove_suid(struct inode *inode,
  1128. struct buffer_head *bh)
  1129. {
  1130. int ret;
  1131. handle_t *handle;
  1132. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1133. struct ocfs2_dinode *di;
  1134. trace_ocfs2_write_remove_suid(
  1135. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1136. inode->i_mode);
  1137. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1138. if (IS_ERR(handle)) {
  1139. ret = PTR_ERR(handle);
  1140. mlog_errno(ret);
  1141. goto out;
  1142. }
  1143. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1144. OCFS2_JOURNAL_ACCESS_WRITE);
  1145. if (ret < 0) {
  1146. mlog_errno(ret);
  1147. goto out_trans;
  1148. }
  1149. inode->i_mode &= ~S_ISUID;
  1150. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1151. inode->i_mode &= ~S_ISGID;
  1152. di = (struct ocfs2_dinode *) bh->b_data;
  1153. di->i_mode = cpu_to_le16(inode->i_mode);
  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_commit_trans(osb, handle);
  1351. out:
  1352. return ret;
  1353. }
  1354. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1355. {
  1356. int i;
  1357. struct ocfs2_extent_rec *rec = NULL;
  1358. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1359. rec = &el->l_recs[i];
  1360. if (le32_to_cpu(rec->e_cpos) < pos)
  1361. break;
  1362. }
  1363. return i;
  1364. }
  1365. /*
  1366. * Helper to calculate the punching pos and length in one run, we handle the
  1367. * following three cases in order:
  1368. *
  1369. * - remove the entire record
  1370. * - remove a partial record
  1371. * - no record needs to be removed (hole-punching completed)
  1372. */
  1373. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1374. struct ocfs2_extent_list *el,
  1375. struct ocfs2_extent_rec *rec,
  1376. u32 trunc_start, u32 *trunc_cpos,
  1377. u32 *trunc_len, u32 *trunc_end,
  1378. u64 *blkno, int *done)
  1379. {
  1380. int ret = 0;
  1381. u32 coff, range;
  1382. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1383. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1384. /*
  1385. * remove an entire extent record.
  1386. */
  1387. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1388. /*
  1389. * Skip holes if any.
  1390. */
  1391. if (range < *trunc_end)
  1392. *trunc_end = range;
  1393. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1394. *blkno = le64_to_cpu(rec->e_blkno);
  1395. *trunc_end = le32_to_cpu(rec->e_cpos);
  1396. } else if (range > trunc_start) {
  1397. /*
  1398. * remove a partial extent record, which means we're
  1399. * removing the last extent record.
  1400. */
  1401. *trunc_cpos = trunc_start;
  1402. /*
  1403. * skip hole if any.
  1404. */
  1405. if (range < *trunc_end)
  1406. *trunc_end = range;
  1407. *trunc_len = *trunc_end - trunc_start;
  1408. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1409. *blkno = le64_to_cpu(rec->e_blkno) +
  1410. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1411. *trunc_end = trunc_start;
  1412. } else {
  1413. /*
  1414. * It may have two following possibilities:
  1415. *
  1416. * - last record has been removed
  1417. * - trunc_start was within a hole
  1418. *
  1419. * both two cases mean the completion of hole punching.
  1420. */
  1421. ret = 1;
  1422. }
  1423. *done = ret;
  1424. }
  1425. static int ocfs2_remove_inode_range(struct inode *inode,
  1426. struct buffer_head *di_bh, u64 byte_start,
  1427. u64 byte_len)
  1428. {
  1429. int ret = 0, flags = 0, done = 0, i;
  1430. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1431. u32 cluster_in_el;
  1432. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1433. struct ocfs2_cached_dealloc_ctxt dealloc;
  1434. struct address_space *mapping = inode->i_mapping;
  1435. struct ocfs2_extent_tree et;
  1436. struct ocfs2_path *path = NULL;
  1437. struct ocfs2_extent_list *el = NULL;
  1438. struct ocfs2_extent_rec *rec = NULL;
  1439. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1440. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1441. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1442. ocfs2_init_dealloc_ctxt(&dealloc);
  1443. trace_ocfs2_remove_inode_range(
  1444. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1445. (unsigned long long)byte_start,
  1446. (unsigned long long)byte_len);
  1447. if (byte_len == 0)
  1448. return 0;
  1449. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1450. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1451. byte_start + byte_len, 0);
  1452. if (ret) {
  1453. mlog_errno(ret);
  1454. goto out;
  1455. }
  1456. /*
  1457. * There's no need to get fancy with the page cache
  1458. * truncate of an inline-data inode. We're talking
  1459. * about less than a page here, which will be cached
  1460. * in the dinode buffer anyway.
  1461. */
  1462. unmap_mapping_range(mapping, 0, 0, 0);
  1463. truncate_inode_pages(mapping, 0);
  1464. goto out;
  1465. }
  1466. /*
  1467. * For reflinks, we may need to CoW 2 clusters which might be
  1468. * partially zero'd later, if hole's start and end offset were
  1469. * within one cluster(means is not exactly aligned to clustersize).
  1470. */
  1471. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  1472. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1473. if (ret) {
  1474. mlog_errno(ret);
  1475. goto out;
  1476. }
  1477. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1478. if (ret) {
  1479. mlog_errno(ret);
  1480. goto out;
  1481. }
  1482. }
  1483. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1484. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1485. cluster_in_el = trunc_end;
  1486. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1487. if (ret) {
  1488. mlog_errno(ret);
  1489. goto out;
  1490. }
  1491. path = ocfs2_new_path_from_et(&et);
  1492. if (!path) {
  1493. ret = -ENOMEM;
  1494. mlog_errno(ret);
  1495. goto out;
  1496. }
  1497. while (trunc_end > trunc_start) {
  1498. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1499. cluster_in_el);
  1500. if (ret) {
  1501. mlog_errno(ret);
  1502. goto out;
  1503. }
  1504. el = path_leaf_el(path);
  1505. i = ocfs2_find_rec(el, trunc_end);
  1506. /*
  1507. * Need to go to previous extent block.
  1508. */
  1509. if (i < 0) {
  1510. if (path->p_tree_depth == 0)
  1511. break;
  1512. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1513. path,
  1514. &cluster_in_el);
  1515. if (ret) {
  1516. mlog_errno(ret);
  1517. goto out;
  1518. }
  1519. /*
  1520. * We've reached the leftmost extent block,
  1521. * it's safe to leave.
  1522. */
  1523. if (cluster_in_el == 0)
  1524. break;
  1525. /*
  1526. * The 'pos' searched for previous extent block is
  1527. * always one cluster less than actual trunc_end.
  1528. */
  1529. trunc_end = cluster_in_el + 1;
  1530. ocfs2_reinit_path(path, 1);
  1531. continue;
  1532. } else
  1533. rec = &el->l_recs[i];
  1534. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1535. &trunc_len, &trunc_end, &blkno, &done);
  1536. if (done)
  1537. break;
  1538. flags = rec->e_flags;
  1539. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1540. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1541. phys_cpos, trunc_len, flags,
  1542. &dealloc, refcount_loc);
  1543. if (ret < 0) {
  1544. mlog_errno(ret);
  1545. goto out;
  1546. }
  1547. cluster_in_el = trunc_end;
  1548. ocfs2_reinit_path(path, 1);
  1549. }
  1550. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1551. out:
  1552. ocfs2_schedule_truncate_log_flush(osb, 1);
  1553. ocfs2_run_deallocs(osb, &dealloc);
  1554. return ret;
  1555. }
  1556. /*
  1557. * Parts of this function taken from xfs_change_file_space()
  1558. */
  1559. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1560. loff_t f_pos, unsigned int cmd,
  1561. struct ocfs2_space_resv *sr,
  1562. int change_size)
  1563. {
  1564. int ret;
  1565. s64 llen;
  1566. loff_t size;
  1567. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1568. struct buffer_head *di_bh = NULL;
  1569. handle_t *handle;
  1570. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1571. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1572. return -EROFS;
  1573. mutex_lock(&inode->i_mutex);
  1574. /*
  1575. * This prevents concurrent writes on other nodes
  1576. */
  1577. ret = ocfs2_rw_lock(inode, 1);
  1578. if (ret) {
  1579. mlog_errno(ret);
  1580. goto out;
  1581. }
  1582. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1583. if (ret) {
  1584. mlog_errno(ret);
  1585. goto out_rw_unlock;
  1586. }
  1587. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1588. ret = -EPERM;
  1589. goto out_inode_unlock;
  1590. }
  1591. switch (sr->l_whence) {
  1592. case 0: /*SEEK_SET*/
  1593. break;
  1594. case 1: /*SEEK_CUR*/
  1595. sr->l_start += f_pos;
  1596. break;
  1597. case 2: /*SEEK_END*/
  1598. sr->l_start += i_size_read(inode);
  1599. break;
  1600. default:
  1601. ret = -EINVAL;
  1602. goto out_inode_unlock;
  1603. }
  1604. sr->l_whence = 0;
  1605. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1606. if (sr->l_start < 0
  1607. || sr->l_start > max_off
  1608. || (sr->l_start + llen) < 0
  1609. || (sr->l_start + llen) > max_off) {
  1610. ret = -EINVAL;
  1611. goto out_inode_unlock;
  1612. }
  1613. size = sr->l_start + sr->l_len;
  1614. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1615. if (sr->l_len <= 0) {
  1616. ret = -EINVAL;
  1617. goto out_inode_unlock;
  1618. }
  1619. }
  1620. if (file && should_remove_suid(file->f_path.dentry)) {
  1621. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1622. if (ret) {
  1623. mlog_errno(ret);
  1624. goto out_inode_unlock;
  1625. }
  1626. }
  1627. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1628. switch (cmd) {
  1629. case OCFS2_IOC_RESVSP:
  1630. case OCFS2_IOC_RESVSP64:
  1631. /*
  1632. * This takes unsigned offsets, but the signed ones we
  1633. * pass have been checked against overflow above.
  1634. */
  1635. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1636. sr->l_len);
  1637. break;
  1638. case OCFS2_IOC_UNRESVSP:
  1639. case OCFS2_IOC_UNRESVSP64:
  1640. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1641. sr->l_len);
  1642. break;
  1643. default:
  1644. ret = -EINVAL;
  1645. }
  1646. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1647. if (ret) {
  1648. mlog_errno(ret);
  1649. goto out_inode_unlock;
  1650. }
  1651. /*
  1652. * We update c/mtime for these changes
  1653. */
  1654. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1655. if (IS_ERR(handle)) {
  1656. ret = PTR_ERR(handle);
  1657. mlog_errno(ret);
  1658. goto out_inode_unlock;
  1659. }
  1660. if (change_size && i_size_read(inode) < size)
  1661. i_size_write(inode, size);
  1662. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1663. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1664. if (ret < 0)
  1665. mlog_errno(ret);
  1666. if (file && (file->f_flags & O_SYNC))
  1667. handle->h_sync = 1;
  1668. ocfs2_commit_trans(osb, handle);
  1669. out_inode_unlock:
  1670. brelse(di_bh);
  1671. ocfs2_inode_unlock(inode, 1);
  1672. out_rw_unlock:
  1673. ocfs2_rw_unlock(inode, 1);
  1674. out:
  1675. mutex_unlock(&inode->i_mutex);
  1676. return ret;
  1677. }
  1678. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1679. struct ocfs2_space_resv *sr)
  1680. {
  1681. struct inode *inode = file->f_path.dentry->d_inode;
  1682. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1683. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1684. !ocfs2_writes_unwritten_extents(osb))
  1685. return -ENOTTY;
  1686. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1687. !ocfs2_sparse_alloc(osb))
  1688. return -ENOTTY;
  1689. if (!S_ISREG(inode->i_mode))
  1690. return -EINVAL;
  1691. if (!(file->f_mode & FMODE_WRITE))
  1692. return -EBADF;
  1693. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1694. }
  1695. static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
  1696. loff_t len)
  1697. {
  1698. struct inode *inode = file->f_path.dentry->d_inode;
  1699. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1700. struct ocfs2_space_resv sr;
  1701. int change_size = 1;
  1702. int cmd = OCFS2_IOC_RESVSP64;
  1703. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1704. return -EOPNOTSUPP;
  1705. if (!ocfs2_writes_unwritten_extents(osb))
  1706. return -EOPNOTSUPP;
  1707. if (mode & FALLOC_FL_KEEP_SIZE)
  1708. change_size = 0;
  1709. if (mode & FALLOC_FL_PUNCH_HOLE)
  1710. cmd = OCFS2_IOC_UNRESVSP64;
  1711. sr.l_whence = 0;
  1712. sr.l_start = (s64)offset;
  1713. sr.l_len = (s64)len;
  1714. return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
  1715. change_size);
  1716. }
  1717. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1718. size_t count)
  1719. {
  1720. int ret = 0;
  1721. unsigned int extent_flags;
  1722. u32 cpos, clusters, extent_len, phys_cpos;
  1723. struct super_block *sb = inode->i_sb;
  1724. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1725. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1726. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1727. return 0;
  1728. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1729. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1730. while (clusters) {
  1731. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1732. &extent_flags);
  1733. if (ret < 0) {
  1734. mlog_errno(ret);
  1735. goto out;
  1736. }
  1737. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1738. ret = 1;
  1739. break;
  1740. }
  1741. if (extent_len > clusters)
  1742. extent_len = clusters;
  1743. clusters -= extent_len;
  1744. cpos += extent_len;
  1745. }
  1746. out:
  1747. return ret;
  1748. }
  1749. static void ocfs2_aiodio_wait(struct inode *inode)
  1750. {
  1751. wait_queue_head_t *wq = ocfs2_ioend_wq(inode);
  1752. wait_event(*wq, (atomic_read(&OCFS2_I(inode)->ip_unaligned_aio) == 0));
  1753. }
  1754. static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
  1755. {
  1756. int blockmask = inode->i_sb->s_blocksize - 1;
  1757. loff_t final_size = pos + count;
  1758. if ((pos & blockmask) || (final_size & blockmask))
  1759. return 1;
  1760. return 0;
  1761. }
  1762. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1763. struct file *file,
  1764. loff_t pos, size_t count,
  1765. int *meta_level)
  1766. {
  1767. int ret;
  1768. struct buffer_head *di_bh = NULL;
  1769. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1770. u32 clusters =
  1771. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1772. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1773. if (ret) {
  1774. mlog_errno(ret);
  1775. goto out;
  1776. }
  1777. *meta_level = 1;
  1778. ret = ocfs2_refcount_cow(inode, file, di_bh, cpos, clusters, UINT_MAX);
  1779. if (ret)
  1780. mlog_errno(ret);
  1781. out:
  1782. brelse(di_bh);
  1783. return ret;
  1784. }
  1785. static int ocfs2_prepare_inode_for_write(struct file *file,
  1786. loff_t *ppos,
  1787. size_t count,
  1788. int appending,
  1789. int *direct_io,
  1790. int *has_refcount)
  1791. {
  1792. int ret = 0, meta_level = 0;
  1793. struct dentry *dentry = file->f_path.dentry;
  1794. struct inode *inode = dentry->d_inode;
  1795. loff_t saved_pos = 0, end;
  1796. /*
  1797. * We start with a read level meta lock and only jump to an ex
  1798. * if we need to make modifications here.
  1799. */
  1800. for(;;) {
  1801. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1802. if (ret < 0) {
  1803. meta_level = -1;
  1804. mlog_errno(ret);
  1805. goto out;
  1806. }
  1807. /* Clear suid / sgid if necessary. We do this here
  1808. * instead of later in the write path because
  1809. * remove_suid() calls ->setattr without any hint that
  1810. * we may have already done our cluster locking. Since
  1811. * ocfs2_setattr() *must* take cluster locks to
  1812. * proceed, this will lead us to recursively lock the
  1813. * inode. There's also the dinode i_size state which
  1814. * can be lost via setattr during extending writes (we
  1815. * set inode->i_size at the end of a write. */
  1816. if (should_remove_suid(dentry)) {
  1817. if (meta_level == 0) {
  1818. ocfs2_inode_unlock(inode, meta_level);
  1819. meta_level = 1;
  1820. continue;
  1821. }
  1822. ret = ocfs2_write_remove_suid(inode);
  1823. if (ret < 0) {
  1824. mlog_errno(ret);
  1825. goto out_unlock;
  1826. }
  1827. }
  1828. /* work on a copy of ppos until we're sure that we won't have
  1829. * to recalculate it due to relocking. */
  1830. if (appending)
  1831. saved_pos = i_size_read(inode);
  1832. else
  1833. saved_pos = *ppos;
  1834. end = saved_pos + count;
  1835. ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
  1836. if (ret == 1) {
  1837. ocfs2_inode_unlock(inode, meta_level);
  1838. meta_level = -1;
  1839. ret = ocfs2_prepare_inode_for_refcount(inode,
  1840. file,
  1841. saved_pos,
  1842. count,
  1843. &meta_level);
  1844. if (has_refcount)
  1845. *has_refcount = 1;
  1846. if (direct_io)
  1847. *direct_io = 0;
  1848. }
  1849. if (ret < 0) {
  1850. mlog_errno(ret);
  1851. goto out_unlock;
  1852. }
  1853. /*
  1854. * Skip the O_DIRECT checks if we don't need
  1855. * them.
  1856. */
  1857. if (!direct_io || !(*direct_io))
  1858. break;
  1859. /*
  1860. * There's no sane way to do direct writes to an inode
  1861. * with inline data.
  1862. */
  1863. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1864. *direct_io = 0;
  1865. break;
  1866. }
  1867. /*
  1868. * Allowing concurrent direct writes means
  1869. * i_size changes wouldn't be synchronized, so
  1870. * one node could wind up truncating another
  1871. * nodes writes.
  1872. */
  1873. if (end > i_size_read(inode)) {
  1874. *direct_io = 0;
  1875. break;
  1876. }
  1877. /*
  1878. * We don't fill holes during direct io, so
  1879. * check for them here. If any are found, the
  1880. * caller will have to retake some cluster
  1881. * locks and initiate the io as buffered.
  1882. */
  1883. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1884. if (ret == 1) {
  1885. *direct_io = 0;
  1886. ret = 0;
  1887. } else if (ret < 0)
  1888. mlog_errno(ret);
  1889. break;
  1890. }
  1891. if (appending)
  1892. *ppos = saved_pos;
  1893. out_unlock:
  1894. trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
  1895. saved_pos, appending, count,
  1896. direct_io, has_refcount);
  1897. if (meta_level >= 0)
  1898. ocfs2_inode_unlock(inode, meta_level);
  1899. out:
  1900. return ret;
  1901. }
  1902. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1903. const struct iovec *iov,
  1904. unsigned long nr_segs,
  1905. loff_t pos)
  1906. {
  1907. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1908. int can_do_direct, has_refcount = 0;
  1909. ssize_t written = 0;
  1910. size_t ocount; /* original count */
  1911. size_t count; /* after file limit checks */
  1912. loff_t old_size, *ppos = &iocb->ki_pos;
  1913. u32 old_clusters;
  1914. struct file *file = iocb->ki_filp;
  1915. struct inode *inode = file->f_path.dentry->d_inode;
  1916. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1917. int full_coherency = !(osb->s_mount_opt &
  1918. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1919. int unaligned_dio = 0;
  1920. trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
  1921. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1922. file->f_path.dentry->d_name.len,
  1923. file->f_path.dentry->d_name.name,
  1924. (unsigned int)nr_segs);
  1925. if (iocb->ki_left == 0)
  1926. return 0;
  1927. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1928. appending = file->f_flags & O_APPEND ? 1 : 0;
  1929. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1930. mutex_lock(&inode->i_mutex);
  1931. ocfs2_iocb_clear_sem_locked(iocb);
  1932. relock:
  1933. /* to match setattr's i_mutex -> rw_lock ordering */
  1934. if (direct_io) {
  1935. have_alloc_sem = 1;
  1936. /* communicate with ocfs2_dio_end_io */
  1937. ocfs2_iocb_set_sem_locked(iocb);
  1938. }
  1939. /*
  1940. * Concurrent O_DIRECT writes are allowed with
  1941. * mount_option "coherency=buffered".
  1942. */
  1943. rw_level = (!direct_io || full_coherency);
  1944. ret = ocfs2_rw_lock(inode, rw_level);
  1945. if (ret < 0) {
  1946. mlog_errno(ret);
  1947. goto out_sems;
  1948. }
  1949. /*
  1950. * O_DIRECT writes with "coherency=full" need to take EX cluster
  1951. * inode_lock to guarantee coherency.
  1952. */
  1953. if (direct_io && full_coherency) {
  1954. /*
  1955. * We need to take and drop the inode lock to force
  1956. * other nodes to drop their caches. Buffered I/O
  1957. * already does this in write_begin().
  1958. */
  1959. ret = ocfs2_inode_lock(inode, NULL, 1);
  1960. if (ret < 0) {
  1961. mlog_errno(ret);
  1962. goto out_sems;
  1963. }
  1964. ocfs2_inode_unlock(inode, 1);
  1965. }
  1966. can_do_direct = direct_io;
  1967. ret = ocfs2_prepare_inode_for_write(file, ppos,
  1968. iocb->ki_left, appending,
  1969. &can_do_direct, &has_refcount);
  1970. if (ret < 0) {
  1971. mlog_errno(ret);
  1972. goto out;
  1973. }
  1974. if (direct_io && !is_sync_kiocb(iocb))
  1975. unaligned_dio = ocfs2_is_io_unaligned(inode, iocb->ki_left,
  1976. *ppos);
  1977. /*
  1978. * We can't complete the direct I/O as requested, fall back to
  1979. * buffered I/O.
  1980. */
  1981. if (direct_io && !can_do_direct) {
  1982. ocfs2_rw_unlock(inode, rw_level);
  1983. have_alloc_sem = 0;
  1984. rw_level = -1;
  1985. direct_io = 0;
  1986. goto relock;
  1987. }
  1988. if (unaligned_dio) {
  1989. /*
  1990. * Wait on previous unaligned aio to complete before
  1991. * proceeding.
  1992. */
  1993. ocfs2_aiodio_wait(inode);
  1994. /* Mark the iocb as needing a decrement in ocfs2_dio_end_io */
  1995. atomic_inc(&OCFS2_I(inode)->ip_unaligned_aio);
  1996. ocfs2_iocb_set_unaligned_aio(iocb);
  1997. }
  1998. /*
  1999. * To later detect whether a journal commit for sync writes is
  2000. * necessary, we sample i_size, and cluster count here.
  2001. */
  2002. old_size = i_size_read(inode);
  2003. old_clusters = OCFS2_I(inode)->ip_clusters;
  2004. /* communicate with ocfs2_dio_end_io */
  2005. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2006. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  2007. VERIFY_READ);
  2008. if (ret)
  2009. goto out_dio;
  2010. count = ocount;
  2011. ret = generic_write_checks(file, ppos, &count,
  2012. S_ISBLK(inode->i_mode));
  2013. if (ret)
  2014. goto out_dio;
  2015. if (direct_io) {
  2016. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  2017. ppos, 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_file_buffered_write(iocb, iov, nr_segs, *ppos,
  2025. ppos, count, 0);
  2026. current->backing_dev_info = NULL;
  2027. }
  2028. out_dio:
  2029. /* buffered aio wouldn't have proper lock coverage today */
  2030. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  2031. if (unlikely(written <= 0))
  2032. goto no_sync;
  2033. if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
  2034. ((file->f_flags & O_DIRECT) && !direct_io)) {
  2035. ret = filemap_fdatawrite_range(file->f_mapping,
  2036. iocb->ki_pos - written,
  2037. iocb->ki_pos - 1);
  2038. if (ret < 0)
  2039. written = ret;
  2040. if (!ret && ((old_size != i_size_read(inode)) ||
  2041. (old_clusters != OCFS2_I(inode)->ip_clusters) ||
  2042. has_refcount)) {
  2043. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  2044. if (ret < 0)
  2045. written = ret;
  2046. }
  2047. if (!ret)
  2048. ret = filemap_fdatawait_range(file->f_mapping,
  2049. iocb->ki_pos - written,
  2050. iocb->ki_pos - 1);
  2051. }
  2052. no_sync:
  2053. /*
  2054. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  2055. * function pointer which is called when o_direct io completes so that
  2056. * it can unlock our rw lock.
  2057. * Unfortunately there are error cases which call end_io and others
  2058. * that don't. so we don't have to unlock the rw_lock if either an
  2059. * async dio is going to do it in the future or an end_io after an
  2060. * error has already done it.
  2061. */
  2062. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  2063. rw_level = -1;
  2064. have_alloc_sem = 0;
  2065. unaligned_dio = 0;
  2066. }
  2067. if (unaligned_dio) {
  2068. ocfs2_iocb_clear_unaligned_aio(iocb);
  2069. atomic_dec(&OCFS2_I(inode)->ip_unaligned_aio);
  2070. }
  2071. out:
  2072. if (rw_level != -1)
  2073. ocfs2_rw_unlock(inode, rw_level);
  2074. out_sems:
  2075. if (have_alloc_sem)
  2076. ocfs2_iocb_clear_sem_locked(iocb);
  2077. mutex_unlock(&inode->i_mutex);
  2078. if (written)
  2079. ret = written;
  2080. return ret;
  2081. }
  2082. static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
  2083. struct file *out,
  2084. struct splice_desc *sd)
  2085. {
  2086. int ret;
  2087. ret = ocfs2_prepare_inode_for_write(out, &sd->pos,
  2088. sd->total_len, 0, NULL, NULL);
  2089. if (ret < 0) {
  2090. mlog_errno(ret);
  2091. return ret;
  2092. }
  2093. return splice_from_pipe_feed(pipe, sd, pipe_to_file);
  2094. }
  2095. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  2096. struct file *out,
  2097. loff_t *ppos,
  2098. size_t len,
  2099. unsigned int flags)
  2100. {
  2101. int ret;
  2102. struct address_space *mapping = out->f_mapping;
  2103. struct inode *inode = mapping->host;
  2104. struct splice_desc sd = {
  2105. .flags = flags,
  2106. .u.file = out,
  2107. };
  2108. trace_ocfs2_file_splice_write(inode, out, out->f_path.dentry,
  2109. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2110. out->f_path.dentry->d_name.len,
  2111. out->f_path.dentry->d_name.name, len);
  2112. ret = generic_write_checks(out, ppos, &len, 0);
  2113. if (ret)
  2114. return ret;
  2115. sd.total_len = len;
  2116. sd.pos = *ppos;
  2117. if (pipe->inode)
  2118. mutex_lock_nested(&pipe->inode->i_mutex, I_MUTEX_PARENT);
  2119. splice_from_pipe_begin(&sd);
  2120. do {
  2121. ret = splice_from_pipe_next(pipe, &sd);
  2122. if (ret <= 0)
  2123. break;
  2124. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2125. ret = ocfs2_rw_lock(inode, 1);
  2126. if (ret < 0)
  2127. mlog_errno(ret);
  2128. else {
  2129. ret = ocfs2_splice_to_file(pipe, out, &sd);
  2130. ocfs2_rw_unlock(inode, 1);
  2131. }
  2132. mutex_unlock(&inode->i_mutex);
  2133. } while (ret > 0);
  2134. splice_from_pipe_end(pipe, &sd);
  2135. if (pipe->inode)
  2136. mutex_unlock(&pipe->inode->i_mutex);
  2137. if (sd.num_spliced)
  2138. ret = sd.num_spliced;
  2139. if (ret > 0) {
  2140. unsigned long nr_pages;
  2141. int err;
  2142. nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  2143. err = generic_write_sync(out, *ppos, ret);
  2144. if (err)
  2145. ret = err;
  2146. else
  2147. *ppos += ret;
  2148. balance_dirty_pages_ratelimited_nr(mapping, nr_pages);
  2149. }
  2150. return ret;
  2151. }
  2152. static ssize_t ocfs2_file_splice_read(struct file *in,
  2153. loff_t *ppos,
  2154. struct pipe_inode_info *pipe,
  2155. size_t len,
  2156. unsigned int flags)
  2157. {
  2158. int ret = 0, lock_level = 0;
  2159. struct inode *inode = in->f_path.dentry->d_inode;
  2160. trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
  2161. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2162. in->f_path.dentry->d_name.len,
  2163. in->f_path.dentry->d_name.name, len);
  2164. /*
  2165. * See the comment in ocfs2_file_aio_read()
  2166. */
  2167. ret = ocfs2_inode_lock_atime(inode, in->f_vfsmnt, &lock_level);
  2168. if (ret < 0) {
  2169. mlog_errno(ret);
  2170. goto bail;
  2171. }
  2172. ocfs2_inode_unlock(inode, lock_level);
  2173. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  2174. bail:
  2175. return ret;
  2176. }
  2177. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  2178. const struct iovec *iov,
  2179. unsigned long nr_segs,
  2180. loff_t pos)
  2181. {
  2182. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  2183. struct file *filp = iocb->ki_filp;
  2184. struct inode *inode = filp->f_path.dentry->d_inode;
  2185. trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
  2186. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2187. filp->f_path.dentry->d_name.len,
  2188. filp->f_path.dentry->d_name.name, nr_segs);
  2189. if (!inode) {
  2190. ret = -EINVAL;
  2191. mlog_errno(ret);
  2192. goto bail;
  2193. }
  2194. ocfs2_iocb_clear_sem_locked(iocb);
  2195. /*
  2196. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  2197. * need locks to protect pending reads from racing with truncate.
  2198. */
  2199. if (filp->f_flags & O_DIRECT) {
  2200. have_alloc_sem = 1;
  2201. ocfs2_iocb_set_sem_locked(iocb);
  2202. ret = ocfs2_rw_lock(inode, 0);
  2203. if (ret < 0) {
  2204. mlog_errno(ret);
  2205. goto bail;
  2206. }
  2207. rw_level = 0;
  2208. /* communicate with ocfs2_dio_end_io */
  2209. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2210. }
  2211. /*
  2212. * We're fine letting folks race truncates and extending
  2213. * writes with read across the cluster, just like they can
  2214. * locally. Hence no rw_lock during read.
  2215. *
  2216. * Take and drop the meta data lock to update inode fields
  2217. * like i_size. This allows the checks down below
  2218. * generic_file_aio_read() a chance of actually working.
  2219. */
  2220. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  2221. if (ret < 0) {
  2222. mlog_errno(ret);
  2223. goto bail;
  2224. }
  2225. ocfs2_inode_unlock(inode, lock_level);
  2226. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  2227. trace_generic_file_aio_read_ret(ret);
  2228. /* buffered aio wouldn't have proper lock coverage today */
  2229. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  2230. /* see ocfs2_file_aio_write */
  2231. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2232. rw_level = -1;
  2233. have_alloc_sem = 0;
  2234. }
  2235. bail:
  2236. if (have_alloc_sem)
  2237. ocfs2_iocb_clear_sem_locked(iocb);
  2238. if (rw_level != -1)
  2239. ocfs2_rw_unlock(inode, rw_level);
  2240. return ret;
  2241. }
  2242. /* Refer generic_file_llseek_unlocked() */
  2243. static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int origin)
  2244. {
  2245. struct inode *inode = file->f_mapping->host;
  2246. int ret = 0;
  2247. mutex_lock(&inode->i_mutex);
  2248. switch (origin) {
  2249. case SEEK_SET:
  2250. break;
  2251. case SEEK_END:
  2252. offset += inode->i_size;
  2253. break;
  2254. case SEEK_CUR:
  2255. if (offset == 0) {
  2256. offset = file->f_pos;
  2257. goto out;
  2258. }
  2259. offset += file->f_pos;
  2260. break;
  2261. case SEEK_DATA:
  2262. case SEEK_HOLE:
  2263. ret = ocfs2_seek_data_hole_offset(file, &offset, origin);
  2264. if (ret)
  2265. goto out;
  2266. break;
  2267. default:
  2268. ret = -EINVAL;
  2269. goto out;
  2270. }
  2271. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
  2272. ret = -EINVAL;
  2273. if (!ret && offset > inode->i_sb->s_maxbytes)
  2274. ret = -EINVAL;
  2275. if (ret)
  2276. goto out;
  2277. if (offset != file->f_pos) {
  2278. file->f_pos = offset;
  2279. file->f_version = 0;
  2280. }
  2281. out:
  2282. mutex_unlock(&inode->i_mutex);
  2283. if (ret)
  2284. return ret;
  2285. return offset;
  2286. }
  2287. const struct inode_operations ocfs2_file_iops = {
  2288. .setattr = ocfs2_setattr,
  2289. .getattr = ocfs2_getattr,
  2290. .permission = ocfs2_permission,
  2291. .setxattr = generic_setxattr,
  2292. .getxattr = generic_getxattr,
  2293. .listxattr = ocfs2_listxattr,
  2294. .removexattr = generic_removexattr,
  2295. .fiemap = ocfs2_fiemap,
  2296. .get_acl = ocfs2_iop_get_acl,
  2297. };
  2298. const struct inode_operations ocfs2_special_file_iops = {
  2299. .setattr = ocfs2_setattr,
  2300. .getattr = ocfs2_getattr,
  2301. .permission = ocfs2_permission,
  2302. .get_acl = ocfs2_iop_get_acl,
  2303. };
  2304. /*
  2305. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2306. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2307. */
  2308. const struct file_operations ocfs2_fops = {
  2309. .llseek = ocfs2_file_llseek,
  2310. .read = do_sync_read,
  2311. .write = do_sync_write,
  2312. .mmap = ocfs2_mmap,
  2313. .fsync = ocfs2_sync_file,
  2314. .release = ocfs2_file_release,
  2315. .open = ocfs2_file_open,
  2316. .aio_read = ocfs2_file_aio_read,
  2317. .aio_write = ocfs2_file_aio_write,
  2318. .unlocked_ioctl = ocfs2_ioctl,
  2319. #ifdef CONFIG_COMPAT
  2320. .compat_ioctl = ocfs2_compat_ioctl,
  2321. #endif
  2322. .lock = ocfs2_lock,
  2323. .flock = ocfs2_flock,
  2324. .splice_read = ocfs2_file_splice_read,
  2325. .splice_write = ocfs2_file_splice_write,
  2326. .fallocate = ocfs2_fallocate,
  2327. };
  2328. const struct file_operations ocfs2_dops = {
  2329. .llseek = generic_file_llseek,
  2330. .read = generic_read_dir,
  2331. .readdir = ocfs2_readdir,
  2332. .fsync = ocfs2_sync_file,
  2333. .release = ocfs2_dir_release,
  2334. .open = ocfs2_dir_open,
  2335. .unlocked_ioctl = ocfs2_ioctl,
  2336. #ifdef CONFIG_COMPAT
  2337. .compat_ioctl = ocfs2_compat_ioctl,
  2338. #endif
  2339. .lock = ocfs2_lock,
  2340. .flock = ocfs2_flock,
  2341. };
  2342. /*
  2343. * POSIX-lockless variants of our file_operations.
  2344. *
  2345. * These will be used if the underlying cluster stack does not support
  2346. * posix file locking, if the user passes the "localflocks" mount
  2347. * option, or if we have a local-only fs.
  2348. *
  2349. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2350. * so we still want it in the case of no stack support for
  2351. * plocks. Internally, it will do the right thing when asked to ignore
  2352. * the cluster.
  2353. */
  2354. const struct file_operations ocfs2_fops_no_plocks = {
  2355. .llseek = ocfs2_file_llseek,
  2356. .read = do_sync_read,
  2357. .write = do_sync_write,
  2358. .mmap = ocfs2_mmap,
  2359. .fsync = ocfs2_sync_file,
  2360. .release = ocfs2_file_release,
  2361. .open = ocfs2_file_open,
  2362. .aio_read = ocfs2_file_aio_read,
  2363. .aio_write = ocfs2_file_aio_write,
  2364. .unlocked_ioctl = ocfs2_ioctl,
  2365. #ifdef CONFIG_COMPAT
  2366. .compat_ioctl = ocfs2_compat_ioctl,
  2367. #endif
  2368. .flock = ocfs2_flock,
  2369. .splice_read = ocfs2_file_splice_read,
  2370. .splice_write = ocfs2_file_splice_write,
  2371. .fallocate = ocfs2_fallocate,
  2372. };
  2373. const struct file_operations ocfs2_dops_no_plocks = {
  2374. .llseek = generic_file_llseek,
  2375. .read = generic_read_dir,
  2376. .readdir = ocfs2_readdir,
  2377. .fsync = ocfs2_sync_file,
  2378. .release = ocfs2_dir_release,
  2379. .open = ocfs2_dir_open,
  2380. .unlocked_ioctl = ocfs2_ioctl,
  2381. #ifdef CONFIG_COMPAT
  2382. .compat_ioctl = ocfs2_compat_ioctl,
  2383. #endif
  2384. .flock = ocfs2_flock,
  2385. };