file.c 64 KB

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