file.c 59 KB

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