file.c 66 KB

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