file.c 64 KB

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