file.c 64 KB

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