file.c 56 KB

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