file.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  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. #define MLOG_MASK_PREFIX ML_INODE
  38. #include <cluster/masklog.h>
  39. #include "ocfs2.h"
  40. #include "alloc.h"
  41. #include "aops.h"
  42. #include "dir.h"
  43. #include "dlmglue.h"
  44. #include "extent_map.h"
  45. #include "file.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "ioctl.h"
  49. #include "journal.h"
  50. #include "mmap.h"
  51. #include "suballoc.h"
  52. #include "super.h"
  53. #include "buffer_head_io.h"
  54. static int ocfs2_sync_inode(struct inode *inode)
  55. {
  56. filemap_fdatawrite(inode->i_mapping);
  57. return sync_mapping_buffers(inode->i_mapping);
  58. }
  59. static int ocfs2_file_open(struct inode *inode, struct file *file)
  60. {
  61. int status;
  62. int mode = file->f_flags;
  63. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  64. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  65. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  66. spin_lock(&oi->ip_lock);
  67. /* Check that the inode hasn't been wiped from disk by another
  68. * node. If it hasn't then we're safe as long as we hold the
  69. * spin lock until our increment of open count. */
  70. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  71. spin_unlock(&oi->ip_lock);
  72. status = -ENOENT;
  73. goto leave;
  74. }
  75. if (mode & O_DIRECT)
  76. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  77. oi->ip_open_count++;
  78. spin_unlock(&oi->ip_lock);
  79. status = 0;
  80. leave:
  81. mlog_exit(status);
  82. return status;
  83. }
  84. static int ocfs2_file_release(struct inode *inode, struct file *file)
  85. {
  86. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  87. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  88. file->f_path.dentry->d_name.len,
  89. file->f_path.dentry->d_name.name);
  90. spin_lock(&oi->ip_lock);
  91. if (!--oi->ip_open_count)
  92. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  93. spin_unlock(&oi->ip_lock);
  94. mlog_exit(0);
  95. return 0;
  96. }
  97. static int ocfs2_sync_file(struct file *file,
  98. struct dentry *dentry,
  99. int datasync)
  100. {
  101. int err = 0;
  102. journal_t *journal;
  103. struct inode *inode = dentry->d_inode;
  104. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  105. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  106. dentry->d_name.len, dentry->d_name.name);
  107. err = ocfs2_sync_inode(dentry->d_inode);
  108. if (err)
  109. goto bail;
  110. journal = osb->journal->j_journal;
  111. err = journal_force_commit(journal);
  112. bail:
  113. mlog_exit(err);
  114. return (err < 0) ? -EIO : 0;
  115. }
  116. int ocfs2_should_update_atime(struct inode *inode,
  117. struct vfsmount *vfsmnt)
  118. {
  119. struct timespec now;
  120. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  121. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  122. return 0;
  123. if ((inode->i_flags & S_NOATIME) ||
  124. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  125. return 0;
  126. /*
  127. * We can be called with no vfsmnt structure - NFSD will
  128. * sometimes do this.
  129. *
  130. * Note that our action here is different than touch_atime() -
  131. * if we can't tell whether this is a noatime mount, then we
  132. * don't know whether to trust the value of s_atime_quantum.
  133. */
  134. if (vfsmnt == NULL)
  135. return 0;
  136. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  137. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  138. return 0;
  139. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  140. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  141. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  142. return 1;
  143. return 0;
  144. }
  145. now = CURRENT_TIME;
  146. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  147. return 0;
  148. else
  149. return 1;
  150. }
  151. int ocfs2_update_inode_atime(struct inode *inode,
  152. struct buffer_head *bh)
  153. {
  154. int ret;
  155. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  156. handle_t *handle;
  157. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  158. mlog_entry_void();
  159. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  160. if (handle == NULL) {
  161. ret = -ENOMEM;
  162. mlog_errno(ret);
  163. goto out;
  164. }
  165. ret = ocfs2_journal_access(handle, inode, bh,
  166. OCFS2_JOURNAL_ACCESS_WRITE);
  167. if (ret) {
  168. mlog_errno(ret);
  169. goto out_commit;
  170. }
  171. /*
  172. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  173. * have i_mutex to guard against concurrent changes to other
  174. * inode fields.
  175. */
  176. inode->i_atime = CURRENT_TIME;
  177. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  178. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  179. ret = ocfs2_journal_dirty(handle, bh);
  180. if (ret < 0)
  181. mlog_errno(ret);
  182. out_commit:
  183. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  184. out:
  185. mlog_exit(ret);
  186. return ret;
  187. }
  188. static int ocfs2_set_inode_size(handle_t *handle,
  189. struct inode *inode,
  190. struct buffer_head *fe_bh,
  191. u64 new_i_size)
  192. {
  193. int status;
  194. mlog_entry_void();
  195. i_size_write(inode, new_i_size);
  196. inode->i_blocks = ocfs2_inode_sector_count(inode);
  197. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  198. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  199. if (status < 0) {
  200. mlog_errno(status);
  201. goto bail;
  202. }
  203. bail:
  204. mlog_exit(status);
  205. return status;
  206. }
  207. static int ocfs2_simple_size_update(struct inode *inode,
  208. struct buffer_head *di_bh,
  209. u64 new_i_size)
  210. {
  211. int ret;
  212. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  213. handle_t *handle = NULL;
  214. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  215. if (handle == NULL) {
  216. ret = -ENOMEM;
  217. mlog_errno(ret);
  218. goto out;
  219. }
  220. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  221. new_i_size);
  222. if (ret < 0)
  223. mlog_errno(ret);
  224. ocfs2_commit_trans(osb, handle);
  225. out:
  226. return ret;
  227. }
  228. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  229. struct inode *inode,
  230. struct buffer_head *fe_bh,
  231. u64 new_i_size)
  232. {
  233. int status;
  234. handle_t *handle;
  235. struct ocfs2_dinode *di;
  236. u64 cluster_bytes;
  237. mlog_entry_void();
  238. /* TODO: This needs to actually orphan the inode in this
  239. * transaction. */
  240. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  241. if (IS_ERR(handle)) {
  242. status = PTR_ERR(handle);
  243. mlog_errno(status);
  244. goto out;
  245. }
  246. status = ocfs2_journal_access(handle, inode, fe_bh,
  247. OCFS2_JOURNAL_ACCESS_WRITE);
  248. if (status < 0) {
  249. mlog_errno(status);
  250. goto out_commit;
  251. }
  252. /*
  253. * Do this before setting i_size.
  254. */
  255. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  256. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  257. cluster_bytes);
  258. if (status) {
  259. mlog_errno(status);
  260. goto out_commit;
  261. }
  262. i_size_write(inode, new_i_size);
  263. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  264. di = (struct ocfs2_dinode *) fe_bh->b_data;
  265. di->i_size = cpu_to_le64(new_i_size);
  266. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  267. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  268. status = ocfs2_journal_dirty(handle, fe_bh);
  269. if (status < 0)
  270. mlog_errno(status);
  271. out_commit:
  272. ocfs2_commit_trans(osb, handle);
  273. out:
  274. mlog_exit(status);
  275. return status;
  276. }
  277. static int ocfs2_truncate_file(struct inode *inode,
  278. struct buffer_head *di_bh,
  279. u64 new_i_size)
  280. {
  281. int status = 0;
  282. struct ocfs2_dinode *fe = NULL;
  283. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  284. struct ocfs2_truncate_context *tc = NULL;
  285. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  286. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  287. (unsigned long long)new_i_size);
  288. fe = (struct ocfs2_dinode *) di_bh->b_data;
  289. if (!OCFS2_IS_VALID_DINODE(fe)) {
  290. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  291. status = -EIO;
  292. goto bail;
  293. }
  294. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  295. "Inode %llu, inode i_size = %lld != di "
  296. "i_size = %llu, i_flags = 0x%x\n",
  297. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  298. i_size_read(inode),
  299. (unsigned long long)le64_to_cpu(fe->i_size),
  300. le32_to_cpu(fe->i_flags));
  301. if (new_i_size > le64_to_cpu(fe->i_size)) {
  302. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  303. (unsigned long long)le64_to_cpu(fe->i_size),
  304. (unsigned long long)new_i_size);
  305. status = -EINVAL;
  306. mlog_errno(status);
  307. goto bail;
  308. }
  309. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  310. (unsigned long long)le64_to_cpu(fe->i_blkno),
  311. (unsigned long long)le64_to_cpu(fe->i_size),
  312. (unsigned long long)new_i_size);
  313. /* lets handle the simple truncate cases before doing any more
  314. * cluster locking. */
  315. if (new_i_size == le64_to_cpu(fe->i_size))
  316. goto bail;
  317. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  318. /* This forces other nodes to sync and drop their pages. Do
  319. * this even if we have a truncate without allocation change -
  320. * ocfs2 cluster sizes can be much greater than page size, so
  321. * we have to truncate them anyway. */
  322. status = ocfs2_data_lock(inode, 1);
  323. if (status < 0) {
  324. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  325. mlog_errno(status);
  326. goto bail;
  327. }
  328. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  329. truncate_inode_pages(inode->i_mapping, new_i_size);
  330. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  331. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  332. i_size_read(inode), 0);
  333. if (status)
  334. mlog_errno(status);
  335. goto bail_unlock_data;
  336. }
  337. /* alright, we're going to need to do a full blown alloc size
  338. * change. Orphan the inode so that recovery can complete the
  339. * truncate if necessary. This does the task of marking
  340. * i_size. */
  341. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  342. if (status < 0) {
  343. mlog_errno(status);
  344. goto bail_unlock_data;
  345. }
  346. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  347. if (status < 0) {
  348. mlog_errno(status);
  349. goto bail_unlock_data;
  350. }
  351. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  352. if (status < 0) {
  353. mlog_errno(status);
  354. goto bail_unlock_data;
  355. }
  356. /* TODO: orphan dir cleanup here. */
  357. bail_unlock_data:
  358. ocfs2_data_unlock(inode, 1);
  359. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  360. bail:
  361. mlog_exit(status);
  362. return status;
  363. }
  364. /*
  365. * extend allocation only here.
  366. * we'll update all the disk stuff, and oip->alloc_size
  367. *
  368. * expect stuff to be locked, a transaction started and enough data /
  369. * metadata reservations in the contexts.
  370. *
  371. * Will return -EAGAIN, and a reason if a restart is needed.
  372. * If passed in, *reason will always be set, even in error.
  373. */
  374. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  375. struct inode *inode,
  376. u32 *logical_offset,
  377. u32 clusters_to_add,
  378. int mark_unwritten,
  379. struct buffer_head *fe_bh,
  380. handle_t *handle,
  381. struct ocfs2_alloc_context *data_ac,
  382. struct ocfs2_alloc_context *meta_ac,
  383. enum ocfs2_alloc_restarted *reason_ret)
  384. {
  385. int status = 0;
  386. int free_extents;
  387. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  388. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  389. u32 bit_off, num_bits;
  390. u64 block;
  391. u8 flags = 0;
  392. BUG_ON(!clusters_to_add);
  393. if (mark_unwritten)
  394. flags = OCFS2_EXT_UNWRITTEN;
  395. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  396. if (free_extents < 0) {
  397. status = free_extents;
  398. mlog_errno(status);
  399. goto leave;
  400. }
  401. /* there are two cases which could cause us to EAGAIN in the
  402. * we-need-more-metadata case:
  403. * 1) we haven't reserved *any*
  404. * 2) we are so fragmented, we've needed to add metadata too
  405. * many times. */
  406. if (!free_extents && !meta_ac) {
  407. mlog(0, "we haven't reserved any metadata!\n");
  408. status = -EAGAIN;
  409. reason = RESTART_META;
  410. goto leave;
  411. } else if ((!free_extents)
  412. && (ocfs2_alloc_context_bits_left(meta_ac)
  413. < ocfs2_extend_meta_needed(fe))) {
  414. mlog(0, "filesystem is really fragmented...\n");
  415. status = -EAGAIN;
  416. reason = RESTART_META;
  417. goto leave;
  418. }
  419. status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  420. clusters_to_add, &bit_off, &num_bits);
  421. if (status < 0) {
  422. if (status != -ENOSPC)
  423. mlog_errno(status);
  424. goto leave;
  425. }
  426. BUG_ON(num_bits > clusters_to_add);
  427. /* reserve our write early -- insert_extent may update the inode */
  428. status = ocfs2_journal_access(handle, inode, fe_bh,
  429. OCFS2_JOURNAL_ACCESS_WRITE);
  430. if (status < 0) {
  431. mlog_errno(status);
  432. goto leave;
  433. }
  434. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  435. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  436. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  437. status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
  438. *logical_offset, block, num_bits,
  439. flags, meta_ac);
  440. if (status < 0) {
  441. mlog_errno(status);
  442. goto leave;
  443. }
  444. status = ocfs2_journal_dirty(handle, fe_bh);
  445. if (status < 0) {
  446. mlog_errno(status);
  447. goto leave;
  448. }
  449. clusters_to_add -= num_bits;
  450. *logical_offset += num_bits;
  451. if (clusters_to_add) {
  452. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  453. "%u\n", fe->i_clusters, clusters_to_add);
  454. status = -EAGAIN;
  455. reason = RESTART_TRANS;
  456. }
  457. leave:
  458. mlog_exit(status);
  459. if (reason_ret)
  460. *reason_ret = reason;
  461. return status;
  462. }
  463. /*
  464. * For a given allocation, determine which allocators will need to be
  465. * accessed, and lock them, reserving the appropriate number of bits.
  466. *
  467. * Sparse file systems call this from ocfs2_write_begin_nolock()
  468. * and ocfs2_allocate_unwritten_extents().
  469. *
  470. * File systems which don't support holes call this from
  471. * ocfs2_extend_allocation().
  472. */
  473. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
  474. u32 clusters_to_add, u32 extents_to_split,
  475. struct ocfs2_alloc_context **data_ac,
  476. struct ocfs2_alloc_context **meta_ac)
  477. {
  478. int ret = 0, num_free_extents;
  479. unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  480. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  481. *meta_ac = NULL;
  482. if (data_ac)
  483. *data_ac = NULL;
  484. BUG_ON(clusters_to_add != 0 && data_ac == NULL);
  485. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  486. "clusters_to_add = %u, extents_to_split = %u\n",
  487. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  488. le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
  489. num_free_extents = ocfs2_num_free_extents(osb, inode, di);
  490. if (num_free_extents < 0) {
  491. ret = num_free_extents;
  492. mlog_errno(ret);
  493. goto out;
  494. }
  495. /*
  496. * Sparse allocation file systems need to be more conservative
  497. * with reserving room for expansion - the actual allocation
  498. * happens while we've got a journal handle open so re-taking
  499. * a cluster lock (because we ran out of room for another
  500. * extent) will violate ordering rules.
  501. *
  502. * Most of the time we'll only be seeing this 1 cluster at a time
  503. * anyway.
  504. *
  505. * Always lock for any unwritten extents - we might want to
  506. * add blocks during a split.
  507. */
  508. if (!num_free_extents ||
  509. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
  510. ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
  511. if (ret < 0) {
  512. if (ret != -ENOSPC)
  513. mlog_errno(ret);
  514. goto out;
  515. }
  516. }
  517. if (clusters_to_add == 0)
  518. goto out;
  519. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  520. if (ret < 0) {
  521. if (ret != -ENOSPC)
  522. mlog_errno(ret);
  523. goto out;
  524. }
  525. out:
  526. if (ret) {
  527. if (*meta_ac) {
  528. ocfs2_free_alloc_context(*meta_ac);
  529. *meta_ac = NULL;
  530. }
  531. /*
  532. * We cannot have an error and a non null *data_ac.
  533. */
  534. }
  535. return ret;
  536. }
  537. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  538. u32 clusters_to_add, int mark_unwritten)
  539. {
  540. int status = 0;
  541. int restart_func = 0;
  542. int credits;
  543. u32 prev_clusters;
  544. struct buffer_head *bh = NULL;
  545. struct ocfs2_dinode *fe = NULL;
  546. handle_t *handle = NULL;
  547. struct ocfs2_alloc_context *data_ac = NULL;
  548. struct ocfs2_alloc_context *meta_ac = NULL;
  549. enum ocfs2_alloc_restarted why;
  550. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  551. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  552. /*
  553. * This function only exists for file systems which don't
  554. * support holes.
  555. */
  556. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  557. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  558. OCFS2_BH_CACHED, inode);
  559. if (status < 0) {
  560. mlog_errno(status);
  561. goto leave;
  562. }
  563. fe = (struct ocfs2_dinode *) bh->b_data;
  564. if (!OCFS2_IS_VALID_DINODE(fe)) {
  565. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  566. status = -EIO;
  567. goto leave;
  568. }
  569. restart_all:
  570. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  571. status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
  572. &meta_ac);
  573. if (status) {
  574. mlog_errno(status);
  575. goto leave;
  576. }
  577. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  578. handle = ocfs2_start_trans(osb, credits);
  579. if (IS_ERR(handle)) {
  580. status = PTR_ERR(handle);
  581. handle = NULL;
  582. mlog_errno(status);
  583. goto leave;
  584. }
  585. restarted_transaction:
  586. /* reserve a write to the file entry early on - that we if we
  587. * run out of credits in the allocation path, we can still
  588. * update i_size. */
  589. status = ocfs2_journal_access(handle, inode, bh,
  590. OCFS2_JOURNAL_ACCESS_WRITE);
  591. if (status < 0) {
  592. mlog_errno(status);
  593. goto leave;
  594. }
  595. prev_clusters = OCFS2_I(inode)->ip_clusters;
  596. status = ocfs2_do_extend_allocation(osb,
  597. inode,
  598. &logical_start,
  599. clusters_to_add,
  600. mark_unwritten,
  601. bh,
  602. handle,
  603. data_ac,
  604. meta_ac,
  605. &why);
  606. if ((status < 0) && (status != -EAGAIN)) {
  607. if (status != -ENOSPC)
  608. mlog_errno(status);
  609. goto leave;
  610. }
  611. status = ocfs2_journal_dirty(handle, bh);
  612. if (status < 0) {
  613. mlog_errno(status);
  614. goto leave;
  615. }
  616. spin_lock(&OCFS2_I(inode)->ip_lock);
  617. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  618. spin_unlock(&OCFS2_I(inode)->ip_lock);
  619. if (why != RESTART_NONE && clusters_to_add) {
  620. if (why == RESTART_META) {
  621. mlog(0, "restarting function.\n");
  622. restart_func = 1;
  623. } else {
  624. BUG_ON(why != RESTART_TRANS);
  625. mlog(0, "restarting transaction.\n");
  626. /* TODO: This can be more intelligent. */
  627. credits = ocfs2_calc_extend_credits(osb->sb,
  628. fe,
  629. clusters_to_add);
  630. status = ocfs2_extend_trans(handle, credits);
  631. if (status < 0) {
  632. /* handle still has to be committed at
  633. * this point. */
  634. status = -ENOMEM;
  635. mlog_errno(status);
  636. goto leave;
  637. }
  638. goto restarted_transaction;
  639. }
  640. }
  641. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  642. le32_to_cpu(fe->i_clusters),
  643. (unsigned long long)le64_to_cpu(fe->i_size));
  644. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  645. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  646. leave:
  647. if (handle) {
  648. ocfs2_commit_trans(osb, handle);
  649. handle = NULL;
  650. }
  651. if (data_ac) {
  652. ocfs2_free_alloc_context(data_ac);
  653. data_ac = NULL;
  654. }
  655. if (meta_ac) {
  656. ocfs2_free_alloc_context(meta_ac);
  657. meta_ac = NULL;
  658. }
  659. if ((!status) && restart_func) {
  660. restart_func = 0;
  661. goto restart_all;
  662. }
  663. if (bh) {
  664. brelse(bh);
  665. bh = NULL;
  666. }
  667. mlog_exit(status);
  668. return status;
  669. }
  670. /* Some parts of this taken from generic_cont_expand, which turned out
  671. * to be too fragile to do exactly what we need without us having to
  672. * worry about recursive locking in ->prepare_write() and
  673. * ->commit_write(). */
  674. static int ocfs2_write_zero_page(struct inode *inode,
  675. u64 size)
  676. {
  677. struct address_space *mapping = inode->i_mapping;
  678. struct page *page;
  679. unsigned long index;
  680. unsigned int offset;
  681. handle_t *handle = NULL;
  682. int ret;
  683. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  684. /* ugh. in prepare/commit_write, if from==to==start of block, we
  685. ** skip the prepare. make sure we never send an offset for the start
  686. ** of a block
  687. */
  688. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  689. offset++;
  690. }
  691. index = size >> PAGE_CACHE_SHIFT;
  692. page = grab_cache_page(mapping, index);
  693. if (!page) {
  694. ret = -ENOMEM;
  695. mlog_errno(ret);
  696. goto out;
  697. }
  698. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  699. if (ret < 0) {
  700. mlog_errno(ret);
  701. goto out_unlock;
  702. }
  703. if (ocfs2_should_order_data(inode)) {
  704. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  705. offset);
  706. if (IS_ERR(handle)) {
  707. ret = PTR_ERR(handle);
  708. handle = NULL;
  709. goto out_unlock;
  710. }
  711. }
  712. /* must not update i_size! */
  713. ret = block_commit_write(page, offset, offset);
  714. if (ret < 0)
  715. mlog_errno(ret);
  716. else
  717. ret = 0;
  718. if (handle)
  719. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  720. out_unlock:
  721. unlock_page(page);
  722. page_cache_release(page);
  723. out:
  724. return ret;
  725. }
  726. static int ocfs2_zero_extend(struct inode *inode,
  727. u64 zero_to_size)
  728. {
  729. int ret = 0;
  730. u64 start_off;
  731. struct super_block *sb = inode->i_sb;
  732. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  733. while (start_off < zero_to_size) {
  734. ret = ocfs2_write_zero_page(inode, start_off);
  735. if (ret < 0) {
  736. mlog_errno(ret);
  737. goto out;
  738. }
  739. start_off += sb->s_blocksize;
  740. /*
  741. * Very large extends have the potential to lock up
  742. * the cpu for extended periods of time.
  743. */
  744. cond_resched();
  745. }
  746. out:
  747. return ret;
  748. }
  749. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  750. {
  751. int ret;
  752. u32 clusters_to_add;
  753. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  754. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  755. if (clusters_to_add < oi->ip_clusters)
  756. clusters_to_add = 0;
  757. else
  758. clusters_to_add -= oi->ip_clusters;
  759. if (clusters_to_add) {
  760. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  761. clusters_to_add, 0);
  762. if (ret) {
  763. mlog_errno(ret);
  764. goto out;
  765. }
  766. }
  767. /*
  768. * Call this even if we don't add any clusters to the tree. We
  769. * still need to zero the area between the old i_size and the
  770. * new i_size.
  771. */
  772. ret = ocfs2_zero_extend(inode, zero_to);
  773. if (ret < 0)
  774. mlog_errno(ret);
  775. out:
  776. return ret;
  777. }
  778. static int ocfs2_extend_file(struct inode *inode,
  779. struct buffer_head *di_bh,
  780. u64 new_i_size)
  781. {
  782. int ret = 0, data_locked = 0;
  783. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  784. BUG_ON(!di_bh);
  785. /* setattr sometimes calls us like this. */
  786. if (new_i_size == 0)
  787. goto out;
  788. if (i_size_read(inode) == new_i_size)
  789. goto out;
  790. BUG_ON(new_i_size < i_size_read(inode));
  791. /*
  792. * Fall through for converting inline data, even if the fs
  793. * supports sparse files.
  794. *
  795. * The check for inline data here is legal - nobody can add
  796. * the feature since we have i_mutex. We must check it again
  797. * after acquiring ip_alloc_sem though, as paths like mmap
  798. * might have raced us to converting the inode to extents.
  799. */
  800. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  801. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  802. goto out_update_size;
  803. /*
  804. * protect the pages that ocfs2_zero_extend is going to be
  805. * pulling into the page cache.. we do this before the
  806. * metadata extend so that we don't get into the situation
  807. * where we've extended the metadata but can't get the data
  808. * lock to zero.
  809. */
  810. ret = ocfs2_data_lock(inode, 1);
  811. if (ret < 0) {
  812. mlog_errno(ret);
  813. goto out;
  814. }
  815. data_locked = 1;
  816. /*
  817. * The alloc sem blocks people in read/write from reading our
  818. * allocation until we're done changing it. We depend on
  819. * i_mutex to block other extend/truncate calls while we're
  820. * here.
  821. */
  822. down_write(&oi->ip_alloc_sem);
  823. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  824. /*
  825. * We can optimize small extends by keeping the inodes
  826. * inline data.
  827. */
  828. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  829. up_write(&oi->ip_alloc_sem);
  830. goto out_update_size;
  831. }
  832. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  833. if (ret) {
  834. up_write(&oi->ip_alloc_sem);
  835. mlog_errno(ret);
  836. goto out_unlock;
  837. }
  838. }
  839. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  840. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  841. up_write(&oi->ip_alloc_sem);
  842. if (ret < 0) {
  843. mlog_errno(ret);
  844. goto out_unlock;
  845. }
  846. out_update_size:
  847. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  848. if (ret < 0)
  849. mlog_errno(ret);
  850. out_unlock:
  851. if (data_locked)
  852. ocfs2_data_unlock(inode, 1);
  853. out:
  854. return ret;
  855. }
  856. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  857. {
  858. int status = 0, size_change;
  859. struct inode *inode = dentry->d_inode;
  860. struct super_block *sb = inode->i_sb;
  861. struct ocfs2_super *osb = OCFS2_SB(sb);
  862. struct buffer_head *bh = NULL;
  863. handle_t *handle = NULL;
  864. mlog_entry("(0x%p, '%.*s')\n", dentry,
  865. dentry->d_name.len, dentry->d_name.name);
  866. if (attr->ia_valid & ATTR_MODE)
  867. mlog(0, "mode change: %d\n", attr->ia_mode);
  868. if (attr->ia_valid & ATTR_UID)
  869. mlog(0, "uid change: %d\n", attr->ia_uid);
  870. if (attr->ia_valid & ATTR_GID)
  871. mlog(0, "gid change: %d\n", attr->ia_gid);
  872. if (attr->ia_valid & ATTR_SIZE)
  873. mlog(0, "size change...\n");
  874. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  875. mlog(0, "time change...\n");
  876. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  877. | ATTR_GID | ATTR_UID | ATTR_MODE)
  878. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  879. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  880. return 0;
  881. }
  882. status = inode_change_ok(inode, attr);
  883. if (status)
  884. return status;
  885. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  886. if (size_change) {
  887. status = ocfs2_rw_lock(inode, 1);
  888. if (status < 0) {
  889. mlog_errno(status);
  890. goto bail;
  891. }
  892. }
  893. status = ocfs2_meta_lock(inode, &bh, 1);
  894. if (status < 0) {
  895. if (status != -ENOENT)
  896. mlog_errno(status);
  897. goto bail_unlock_rw;
  898. }
  899. if (size_change && attr->ia_size != i_size_read(inode)) {
  900. if (attr->ia_size > sb->s_maxbytes) {
  901. status = -EFBIG;
  902. goto bail_unlock;
  903. }
  904. if (i_size_read(inode) > attr->ia_size)
  905. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  906. else
  907. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  908. if (status < 0) {
  909. if (status != -ENOSPC)
  910. mlog_errno(status);
  911. status = -ENOSPC;
  912. goto bail_unlock;
  913. }
  914. }
  915. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  916. if (IS_ERR(handle)) {
  917. status = PTR_ERR(handle);
  918. mlog_errno(status);
  919. goto bail_unlock;
  920. }
  921. /*
  922. * This will intentionally not wind up calling vmtruncate(),
  923. * since all the work for a size change has been done above.
  924. * Otherwise, we could get into problems with truncate as
  925. * ip_alloc_sem is used there to protect against i_size
  926. * changes.
  927. */
  928. status = inode_setattr(inode, attr);
  929. if (status < 0) {
  930. mlog_errno(status);
  931. goto bail_commit;
  932. }
  933. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  934. if (status < 0)
  935. mlog_errno(status);
  936. bail_commit:
  937. ocfs2_commit_trans(osb, handle);
  938. bail_unlock:
  939. ocfs2_meta_unlock(inode, 1);
  940. bail_unlock_rw:
  941. if (size_change)
  942. ocfs2_rw_unlock(inode, 1);
  943. bail:
  944. if (bh)
  945. brelse(bh);
  946. mlog_exit(status);
  947. return status;
  948. }
  949. int ocfs2_getattr(struct vfsmount *mnt,
  950. struct dentry *dentry,
  951. struct kstat *stat)
  952. {
  953. struct inode *inode = dentry->d_inode;
  954. struct super_block *sb = dentry->d_inode->i_sb;
  955. struct ocfs2_super *osb = sb->s_fs_info;
  956. int err;
  957. mlog_entry_void();
  958. err = ocfs2_inode_revalidate(dentry);
  959. if (err) {
  960. if (err != -ENOENT)
  961. mlog_errno(err);
  962. goto bail;
  963. }
  964. generic_fillattr(inode, stat);
  965. /* We set the blksize from the cluster size for performance */
  966. stat->blksize = osb->s_clustersize;
  967. bail:
  968. mlog_exit(err);
  969. return err;
  970. }
  971. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  972. {
  973. int ret;
  974. mlog_entry_void();
  975. ret = ocfs2_meta_lock(inode, NULL, 0);
  976. if (ret) {
  977. if (ret != -ENOENT)
  978. mlog_errno(ret);
  979. goto out;
  980. }
  981. ret = generic_permission(inode, mask, NULL);
  982. ocfs2_meta_unlock(inode, 0);
  983. out:
  984. mlog_exit(ret);
  985. return ret;
  986. }
  987. static int __ocfs2_write_remove_suid(struct inode *inode,
  988. struct buffer_head *bh)
  989. {
  990. int ret;
  991. handle_t *handle;
  992. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  993. struct ocfs2_dinode *di;
  994. mlog_entry("(Inode %llu, mode 0%o)\n",
  995. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  996. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  997. if (handle == NULL) {
  998. ret = -ENOMEM;
  999. mlog_errno(ret);
  1000. goto out;
  1001. }
  1002. ret = ocfs2_journal_access(handle, inode, bh,
  1003. OCFS2_JOURNAL_ACCESS_WRITE);
  1004. if (ret < 0) {
  1005. mlog_errno(ret);
  1006. goto out_trans;
  1007. }
  1008. inode->i_mode &= ~S_ISUID;
  1009. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1010. inode->i_mode &= ~S_ISGID;
  1011. di = (struct ocfs2_dinode *) bh->b_data;
  1012. di->i_mode = cpu_to_le16(inode->i_mode);
  1013. ret = ocfs2_journal_dirty(handle, bh);
  1014. if (ret < 0)
  1015. mlog_errno(ret);
  1016. out_trans:
  1017. ocfs2_commit_trans(osb, handle);
  1018. out:
  1019. mlog_exit(ret);
  1020. return ret;
  1021. }
  1022. /*
  1023. * Will look for holes and unwritten extents in the range starting at
  1024. * pos for count bytes (inclusive).
  1025. */
  1026. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1027. size_t count)
  1028. {
  1029. int ret = 0;
  1030. unsigned int extent_flags;
  1031. u32 cpos, clusters, extent_len, phys_cpos;
  1032. struct super_block *sb = inode->i_sb;
  1033. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1034. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1035. while (clusters) {
  1036. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1037. &extent_flags);
  1038. if (ret < 0) {
  1039. mlog_errno(ret);
  1040. goto out;
  1041. }
  1042. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1043. ret = 1;
  1044. break;
  1045. }
  1046. if (extent_len > clusters)
  1047. extent_len = clusters;
  1048. clusters -= extent_len;
  1049. cpos += extent_len;
  1050. }
  1051. out:
  1052. return ret;
  1053. }
  1054. static int ocfs2_write_remove_suid(struct inode *inode)
  1055. {
  1056. int ret;
  1057. struct buffer_head *bh = NULL;
  1058. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1059. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1060. oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  1061. if (ret < 0) {
  1062. mlog_errno(ret);
  1063. goto out;
  1064. }
  1065. ret = __ocfs2_write_remove_suid(inode, bh);
  1066. out:
  1067. brelse(bh);
  1068. return ret;
  1069. }
  1070. /*
  1071. * Allocate enough extents to cover the region starting at byte offset
  1072. * start for len bytes. Existing extents are skipped, any extents
  1073. * added are marked as "unwritten".
  1074. */
  1075. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1076. u64 start, u64 len)
  1077. {
  1078. int ret;
  1079. u32 cpos, phys_cpos, clusters, alloc_size;
  1080. u64 end = start + len;
  1081. struct buffer_head *di_bh = NULL;
  1082. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1083. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1084. OCFS2_I(inode)->ip_blkno, &di_bh,
  1085. OCFS2_BH_CACHED, inode);
  1086. if (ret) {
  1087. mlog_errno(ret);
  1088. goto out;
  1089. }
  1090. /*
  1091. * Nothing to do if the requested reservation range
  1092. * fits within the inode.
  1093. */
  1094. if (ocfs2_size_fits_inline_data(di_bh, end))
  1095. goto out;
  1096. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1097. if (ret) {
  1098. mlog_errno(ret);
  1099. goto out;
  1100. }
  1101. }
  1102. /*
  1103. * We consider both start and len to be inclusive.
  1104. */
  1105. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1106. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1107. clusters -= cpos;
  1108. while (clusters) {
  1109. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1110. &alloc_size, NULL);
  1111. if (ret) {
  1112. mlog_errno(ret);
  1113. goto out;
  1114. }
  1115. /*
  1116. * Hole or existing extent len can be arbitrary, so
  1117. * cap it to our own allocation request.
  1118. */
  1119. if (alloc_size > clusters)
  1120. alloc_size = clusters;
  1121. if (phys_cpos) {
  1122. /*
  1123. * We already have an allocation at this
  1124. * region so we can safely skip it.
  1125. */
  1126. goto next;
  1127. }
  1128. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1129. if (ret) {
  1130. if (ret != -ENOSPC)
  1131. mlog_errno(ret);
  1132. goto out;
  1133. }
  1134. next:
  1135. cpos += alloc_size;
  1136. clusters -= alloc_size;
  1137. }
  1138. ret = 0;
  1139. out:
  1140. brelse(di_bh);
  1141. return ret;
  1142. }
  1143. static int __ocfs2_remove_inode_range(struct inode *inode,
  1144. struct buffer_head *di_bh,
  1145. u32 cpos, u32 phys_cpos, u32 len,
  1146. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1147. {
  1148. int ret;
  1149. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  1150. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1151. struct inode *tl_inode = osb->osb_tl_inode;
  1152. handle_t *handle;
  1153. struct ocfs2_alloc_context *meta_ac = NULL;
  1154. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1155. ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
  1156. if (ret) {
  1157. mlog_errno(ret);
  1158. return ret;
  1159. }
  1160. mutex_lock(&tl_inode->i_mutex);
  1161. if (ocfs2_truncate_log_needs_flush(osb)) {
  1162. ret = __ocfs2_flush_truncate_log(osb);
  1163. if (ret < 0) {
  1164. mlog_errno(ret);
  1165. goto out;
  1166. }
  1167. }
  1168. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  1169. if (handle == NULL) {
  1170. ret = -ENOMEM;
  1171. mlog_errno(ret);
  1172. goto out;
  1173. }
  1174. ret = ocfs2_journal_access(handle, inode, di_bh,
  1175. OCFS2_JOURNAL_ACCESS_WRITE);
  1176. if (ret) {
  1177. mlog_errno(ret);
  1178. goto out;
  1179. }
  1180. ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
  1181. dealloc);
  1182. if (ret) {
  1183. mlog_errno(ret);
  1184. goto out_commit;
  1185. }
  1186. OCFS2_I(inode)->ip_clusters -= len;
  1187. di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1188. ret = ocfs2_journal_dirty(handle, di_bh);
  1189. if (ret) {
  1190. mlog_errno(ret);
  1191. goto out_commit;
  1192. }
  1193. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  1194. if (ret)
  1195. mlog_errno(ret);
  1196. out_commit:
  1197. ocfs2_commit_trans(osb, handle);
  1198. out:
  1199. mutex_unlock(&tl_inode->i_mutex);
  1200. if (meta_ac)
  1201. ocfs2_free_alloc_context(meta_ac);
  1202. return ret;
  1203. }
  1204. /*
  1205. * Truncate a byte range, avoiding pages within partial clusters. This
  1206. * preserves those pages for the zeroing code to write to.
  1207. */
  1208. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1209. u64 byte_len)
  1210. {
  1211. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1212. loff_t start, end;
  1213. struct address_space *mapping = inode->i_mapping;
  1214. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1215. end = byte_start + byte_len;
  1216. end = end & ~(osb->s_clustersize - 1);
  1217. if (start < end) {
  1218. unmap_mapping_range(mapping, start, end - start, 0);
  1219. truncate_inode_pages_range(mapping, start, end - 1);
  1220. }
  1221. }
  1222. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1223. u64 start, u64 len)
  1224. {
  1225. int ret = 0;
  1226. u64 tmpend, end = start + len;
  1227. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1228. unsigned int csize = osb->s_clustersize;
  1229. handle_t *handle;
  1230. /*
  1231. * The "start" and "end" values are NOT necessarily part of
  1232. * the range whose allocation is being deleted. Rather, this
  1233. * is what the user passed in with the request. We must zero
  1234. * partial clusters here. There's no need to worry about
  1235. * physical allocation - the zeroing code knows to skip holes.
  1236. */
  1237. mlog(0, "byte start: %llu, end: %llu\n",
  1238. (unsigned long long)start, (unsigned long long)end);
  1239. /*
  1240. * If both edges are on a cluster boundary then there's no
  1241. * zeroing required as the region is part of the allocation to
  1242. * be truncated.
  1243. */
  1244. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1245. goto out;
  1246. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1247. if (handle == NULL) {
  1248. ret = -ENOMEM;
  1249. mlog_errno(ret);
  1250. goto out;
  1251. }
  1252. /*
  1253. * We want to get the byte offset of the end of the 1st cluster.
  1254. */
  1255. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1256. if (tmpend > end)
  1257. tmpend = end;
  1258. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1259. (unsigned long long)start, (unsigned long long)tmpend);
  1260. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1261. if (ret)
  1262. mlog_errno(ret);
  1263. if (tmpend < end) {
  1264. /*
  1265. * This may make start and end equal, but the zeroing
  1266. * code will skip any work in that case so there's no
  1267. * need to catch it up here.
  1268. */
  1269. start = end & ~(osb->s_clustersize - 1);
  1270. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1271. (unsigned long long)start, (unsigned long long)end);
  1272. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1273. if (ret)
  1274. mlog_errno(ret);
  1275. }
  1276. ocfs2_commit_trans(osb, handle);
  1277. out:
  1278. return ret;
  1279. }
  1280. static int ocfs2_remove_inode_range(struct inode *inode,
  1281. struct buffer_head *di_bh, u64 byte_start,
  1282. u64 byte_len)
  1283. {
  1284. int ret = 0;
  1285. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1286. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1287. struct ocfs2_cached_dealloc_ctxt dealloc;
  1288. ocfs2_init_dealloc_ctxt(&dealloc);
  1289. if (byte_len == 0)
  1290. return 0;
  1291. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1292. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1293. byte_start + byte_len, 1);
  1294. if (ret)
  1295. mlog_errno(ret);
  1296. return ret;
  1297. }
  1298. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1299. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1300. if (trunc_len >= trunc_start)
  1301. trunc_len -= trunc_start;
  1302. else
  1303. trunc_len = 0;
  1304. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1305. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1306. (unsigned long long)byte_start,
  1307. (unsigned long long)byte_len, trunc_start, trunc_len);
  1308. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1309. if (ret) {
  1310. mlog_errno(ret);
  1311. goto out;
  1312. }
  1313. cpos = trunc_start;
  1314. while (trunc_len) {
  1315. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1316. &alloc_size, NULL);
  1317. if (ret) {
  1318. mlog_errno(ret);
  1319. goto out;
  1320. }
  1321. if (alloc_size > trunc_len)
  1322. alloc_size = trunc_len;
  1323. /* Only do work for non-holes */
  1324. if (phys_cpos != 0) {
  1325. ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
  1326. phys_cpos, alloc_size,
  1327. &dealloc);
  1328. if (ret) {
  1329. mlog_errno(ret);
  1330. goto out;
  1331. }
  1332. }
  1333. cpos += alloc_size;
  1334. trunc_len -= alloc_size;
  1335. }
  1336. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1337. out:
  1338. ocfs2_schedule_truncate_log_flush(osb, 1);
  1339. ocfs2_run_deallocs(osb, &dealloc);
  1340. return ret;
  1341. }
  1342. /*
  1343. * Parts of this function taken from xfs_change_file_space()
  1344. */
  1345. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1346. loff_t f_pos, unsigned int cmd,
  1347. struct ocfs2_space_resv *sr,
  1348. int change_size)
  1349. {
  1350. int ret;
  1351. s64 llen;
  1352. loff_t size;
  1353. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1354. struct buffer_head *di_bh = NULL;
  1355. handle_t *handle;
  1356. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1357. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1358. return -EROFS;
  1359. mutex_lock(&inode->i_mutex);
  1360. /*
  1361. * This prevents concurrent writes on other nodes
  1362. */
  1363. ret = ocfs2_rw_lock(inode, 1);
  1364. if (ret) {
  1365. mlog_errno(ret);
  1366. goto out;
  1367. }
  1368. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1369. if (ret) {
  1370. mlog_errno(ret);
  1371. goto out_rw_unlock;
  1372. }
  1373. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1374. ret = -EPERM;
  1375. goto out_meta_unlock;
  1376. }
  1377. switch (sr->l_whence) {
  1378. case 0: /*SEEK_SET*/
  1379. break;
  1380. case 1: /*SEEK_CUR*/
  1381. sr->l_start += f_pos;
  1382. break;
  1383. case 2: /*SEEK_END*/
  1384. sr->l_start += i_size_read(inode);
  1385. break;
  1386. default:
  1387. ret = -EINVAL;
  1388. goto out_meta_unlock;
  1389. }
  1390. sr->l_whence = 0;
  1391. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1392. if (sr->l_start < 0
  1393. || sr->l_start > max_off
  1394. || (sr->l_start + llen) < 0
  1395. || (sr->l_start + llen) > max_off) {
  1396. ret = -EINVAL;
  1397. goto out_meta_unlock;
  1398. }
  1399. size = sr->l_start + sr->l_len;
  1400. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1401. if (sr->l_len <= 0) {
  1402. ret = -EINVAL;
  1403. goto out_meta_unlock;
  1404. }
  1405. }
  1406. if (file && should_remove_suid(file->f_path.dentry)) {
  1407. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1408. if (ret) {
  1409. mlog_errno(ret);
  1410. goto out_meta_unlock;
  1411. }
  1412. }
  1413. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1414. switch (cmd) {
  1415. case OCFS2_IOC_RESVSP:
  1416. case OCFS2_IOC_RESVSP64:
  1417. /*
  1418. * This takes unsigned offsets, but the signed ones we
  1419. * pass have been checked against overflow above.
  1420. */
  1421. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1422. sr->l_len);
  1423. break;
  1424. case OCFS2_IOC_UNRESVSP:
  1425. case OCFS2_IOC_UNRESVSP64:
  1426. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1427. sr->l_len);
  1428. break;
  1429. default:
  1430. ret = -EINVAL;
  1431. }
  1432. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1433. if (ret) {
  1434. mlog_errno(ret);
  1435. goto out_meta_unlock;
  1436. }
  1437. /*
  1438. * We update c/mtime for these changes
  1439. */
  1440. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1441. if (IS_ERR(handle)) {
  1442. ret = PTR_ERR(handle);
  1443. mlog_errno(ret);
  1444. goto out_meta_unlock;
  1445. }
  1446. if (change_size && i_size_read(inode) < size)
  1447. i_size_write(inode, size);
  1448. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1449. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1450. if (ret < 0)
  1451. mlog_errno(ret);
  1452. ocfs2_commit_trans(osb, handle);
  1453. out_meta_unlock:
  1454. brelse(di_bh);
  1455. ocfs2_meta_unlock(inode, 1);
  1456. out_rw_unlock:
  1457. ocfs2_rw_unlock(inode, 1);
  1458. mutex_unlock(&inode->i_mutex);
  1459. out:
  1460. return ret;
  1461. }
  1462. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1463. struct ocfs2_space_resv *sr)
  1464. {
  1465. struct inode *inode = file->f_path.dentry->d_inode;
  1466. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
  1467. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1468. !ocfs2_writes_unwritten_extents(osb))
  1469. return -ENOTTY;
  1470. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1471. !ocfs2_sparse_alloc(osb))
  1472. return -ENOTTY;
  1473. if (!S_ISREG(inode->i_mode))
  1474. return -EINVAL;
  1475. if (!(file->f_mode & FMODE_WRITE))
  1476. return -EBADF;
  1477. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1478. }
  1479. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1480. loff_t len)
  1481. {
  1482. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1483. struct ocfs2_space_resv sr;
  1484. int change_size = 1;
  1485. if (!ocfs2_writes_unwritten_extents(osb))
  1486. return -EOPNOTSUPP;
  1487. if (S_ISDIR(inode->i_mode))
  1488. return -ENODEV;
  1489. if (mode & FALLOC_FL_KEEP_SIZE)
  1490. change_size = 0;
  1491. sr.l_whence = 0;
  1492. sr.l_start = (s64)offset;
  1493. sr.l_len = (s64)len;
  1494. return __ocfs2_change_file_space(NULL, inode, offset,
  1495. OCFS2_IOC_RESVSP64, &sr, change_size);
  1496. }
  1497. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1498. loff_t *ppos,
  1499. size_t count,
  1500. int appending,
  1501. int *direct_io)
  1502. {
  1503. int ret = 0, meta_level = 0;
  1504. struct inode *inode = dentry->d_inode;
  1505. loff_t saved_pos, end;
  1506. /*
  1507. * We start with a read level meta lock and only jump to an ex
  1508. * if we need to make modifications here.
  1509. */
  1510. for(;;) {
  1511. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1512. if (ret < 0) {
  1513. meta_level = -1;
  1514. mlog_errno(ret);
  1515. goto out;
  1516. }
  1517. /* Clear suid / sgid if necessary. We do this here
  1518. * instead of later in the write path because
  1519. * remove_suid() calls ->setattr without any hint that
  1520. * we may have already done our cluster locking. Since
  1521. * ocfs2_setattr() *must* take cluster locks to
  1522. * proceeed, this will lead us to recursively lock the
  1523. * inode. There's also the dinode i_size state which
  1524. * can be lost via setattr during extending writes (we
  1525. * set inode->i_size at the end of a write. */
  1526. if (should_remove_suid(dentry)) {
  1527. if (meta_level == 0) {
  1528. ocfs2_meta_unlock(inode, meta_level);
  1529. meta_level = 1;
  1530. continue;
  1531. }
  1532. ret = ocfs2_write_remove_suid(inode);
  1533. if (ret < 0) {
  1534. mlog_errno(ret);
  1535. goto out_unlock;
  1536. }
  1537. }
  1538. /* work on a copy of ppos until we're sure that we won't have
  1539. * to recalculate it due to relocking. */
  1540. if (appending) {
  1541. saved_pos = i_size_read(inode);
  1542. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1543. } else {
  1544. saved_pos = *ppos;
  1545. }
  1546. end = saved_pos + count;
  1547. /*
  1548. * Skip the O_DIRECT checks if we don't need
  1549. * them.
  1550. */
  1551. if (!direct_io || !(*direct_io))
  1552. break;
  1553. /*
  1554. * There's no sane way to do direct writes to an inode
  1555. * with inline data.
  1556. */
  1557. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1558. *direct_io = 0;
  1559. break;
  1560. }
  1561. /*
  1562. * Allowing concurrent direct writes means
  1563. * i_size changes wouldn't be synchronized, so
  1564. * one node could wind up truncating another
  1565. * nodes writes.
  1566. */
  1567. if (end > i_size_read(inode)) {
  1568. *direct_io = 0;
  1569. break;
  1570. }
  1571. /*
  1572. * We don't fill holes during direct io, so
  1573. * check for them here. If any are found, the
  1574. * caller will have to retake some cluster
  1575. * locks and initiate the io as buffered.
  1576. */
  1577. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1578. if (ret == 1) {
  1579. *direct_io = 0;
  1580. ret = 0;
  1581. } else if (ret < 0)
  1582. mlog_errno(ret);
  1583. break;
  1584. }
  1585. if (appending)
  1586. *ppos = saved_pos;
  1587. out_unlock:
  1588. ocfs2_meta_unlock(inode, meta_level);
  1589. out:
  1590. return ret;
  1591. }
  1592. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1593. const struct iovec *iov,
  1594. unsigned long nr_segs,
  1595. loff_t pos)
  1596. {
  1597. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1598. int can_do_direct;
  1599. ssize_t written = 0;
  1600. size_t ocount; /* original count */
  1601. size_t count; /* after file limit checks */
  1602. loff_t *ppos = &iocb->ki_pos;
  1603. struct file *file = iocb->ki_filp;
  1604. struct inode *inode = file->f_path.dentry->d_inode;
  1605. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1606. (unsigned int)nr_segs,
  1607. file->f_path.dentry->d_name.len,
  1608. file->f_path.dentry->d_name.name);
  1609. if (iocb->ki_left == 0)
  1610. return 0;
  1611. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1612. appending = file->f_flags & O_APPEND ? 1 : 0;
  1613. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1614. mutex_lock(&inode->i_mutex);
  1615. relock:
  1616. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1617. if (direct_io) {
  1618. down_read(&inode->i_alloc_sem);
  1619. have_alloc_sem = 1;
  1620. }
  1621. /* concurrent O_DIRECT writes are allowed */
  1622. rw_level = !direct_io;
  1623. ret = ocfs2_rw_lock(inode, rw_level);
  1624. if (ret < 0) {
  1625. mlog_errno(ret);
  1626. goto out_sems;
  1627. }
  1628. can_do_direct = direct_io;
  1629. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1630. iocb->ki_left, appending,
  1631. &can_do_direct);
  1632. if (ret < 0) {
  1633. mlog_errno(ret);
  1634. goto out;
  1635. }
  1636. /*
  1637. * We can't complete the direct I/O as requested, fall back to
  1638. * buffered I/O.
  1639. */
  1640. if (direct_io && !can_do_direct) {
  1641. ocfs2_rw_unlock(inode, rw_level);
  1642. up_read(&inode->i_alloc_sem);
  1643. have_alloc_sem = 0;
  1644. rw_level = -1;
  1645. direct_io = 0;
  1646. goto relock;
  1647. }
  1648. /* communicate with ocfs2_dio_end_io */
  1649. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1650. if (direct_io) {
  1651. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1652. VERIFY_READ);
  1653. if (ret)
  1654. goto out_dio;
  1655. ret = generic_write_checks(file, ppos, &count,
  1656. S_ISBLK(inode->i_mode));
  1657. if (ret)
  1658. goto out_dio;
  1659. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1660. ppos, count, ocount);
  1661. if (written < 0) {
  1662. ret = written;
  1663. goto out_dio;
  1664. }
  1665. } else {
  1666. written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1667. *ppos);
  1668. }
  1669. out_dio:
  1670. /* buffered aio wouldn't have proper lock coverage today */
  1671. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1672. /*
  1673. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1674. * function pointer which is called when o_direct io completes so that
  1675. * it can unlock our rw lock. (it's the clustered equivalent of
  1676. * i_alloc_sem; protects truncate from racing with pending ios).
  1677. * Unfortunately there are error cases which call end_io and others
  1678. * that don't. so we don't have to unlock the rw_lock if either an
  1679. * async dio is going to do it in the future or an end_io after an
  1680. * error has already done it.
  1681. */
  1682. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1683. rw_level = -1;
  1684. have_alloc_sem = 0;
  1685. }
  1686. out:
  1687. if (rw_level != -1)
  1688. ocfs2_rw_unlock(inode, rw_level);
  1689. out_sems:
  1690. if (have_alloc_sem)
  1691. up_read(&inode->i_alloc_sem);
  1692. mutex_unlock(&inode->i_mutex);
  1693. mlog_exit(ret);
  1694. return written ? written : ret;
  1695. }
  1696. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1697. struct file *out,
  1698. loff_t *ppos,
  1699. size_t len,
  1700. unsigned int flags)
  1701. {
  1702. int ret;
  1703. struct inode *inode = out->f_path.dentry->d_inode;
  1704. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1705. (unsigned int)len,
  1706. out->f_path.dentry->d_name.len,
  1707. out->f_path.dentry->d_name.name);
  1708. inode_double_lock(inode, pipe->inode);
  1709. ret = ocfs2_rw_lock(inode, 1);
  1710. if (ret < 0) {
  1711. mlog_errno(ret);
  1712. goto out;
  1713. }
  1714. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1715. NULL);
  1716. if (ret < 0) {
  1717. mlog_errno(ret);
  1718. goto out_unlock;
  1719. }
  1720. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1721. out_unlock:
  1722. ocfs2_rw_unlock(inode, 1);
  1723. out:
  1724. inode_double_unlock(inode, pipe->inode);
  1725. mlog_exit(ret);
  1726. return ret;
  1727. }
  1728. static ssize_t ocfs2_file_splice_read(struct file *in,
  1729. loff_t *ppos,
  1730. struct pipe_inode_info *pipe,
  1731. size_t len,
  1732. unsigned int flags)
  1733. {
  1734. int ret = 0;
  1735. struct inode *inode = in->f_path.dentry->d_inode;
  1736. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1737. (unsigned int)len,
  1738. in->f_path.dentry->d_name.len,
  1739. in->f_path.dentry->d_name.name);
  1740. /*
  1741. * See the comment in ocfs2_file_aio_read()
  1742. */
  1743. ret = ocfs2_meta_lock(inode, NULL, 0);
  1744. if (ret < 0) {
  1745. mlog_errno(ret);
  1746. goto bail;
  1747. }
  1748. ocfs2_meta_unlock(inode, 0);
  1749. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1750. bail:
  1751. mlog_exit(ret);
  1752. return ret;
  1753. }
  1754. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1755. const struct iovec *iov,
  1756. unsigned long nr_segs,
  1757. loff_t pos)
  1758. {
  1759. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1760. struct file *filp = iocb->ki_filp;
  1761. struct inode *inode = filp->f_path.dentry->d_inode;
  1762. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1763. (unsigned int)nr_segs,
  1764. filp->f_path.dentry->d_name.len,
  1765. filp->f_path.dentry->d_name.name);
  1766. if (!inode) {
  1767. ret = -EINVAL;
  1768. mlog_errno(ret);
  1769. goto bail;
  1770. }
  1771. /*
  1772. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1773. * need locks to protect pending reads from racing with truncate.
  1774. */
  1775. if (filp->f_flags & O_DIRECT) {
  1776. down_read(&inode->i_alloc_sem);
  1777. have_alloc_sem = 1;
  1778. ret = ocfs2_rw_lock(inode, 0);
  1779. if (ret < 0) {
  1780. mlog_errno(ret);
  1781. goto bail;
  1782. }
  1783. rw_level = 0;
  1784. /* communicate with ocfs2_dio_end_io */
  1785. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1786. }
  1787. /*
  1788. * We're fine letting folks race truncates and extending
  1789. * writes with read across the cluster, just like they can
  1790. * locally. Hence no rw_lock during read.
  1791. *
  1792. * Take and drop the meta data lock to update inode fields
  1793. * like i_size. This allows the checks down below
  1794. * generic_file_aio_read() a chance of actually working.
  1795. */
  1796. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1797. if (ret < 0) {
  1798. mlog_errno(ret);
  1799. goto bail;
  1800. }
  1801. ocfs2_meta_unlock(inode, lock_level);
  1802. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1803. if (ret == -EINVAL)
  1804. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1805. /* buffered aio wouldn't have proper lock coverage today */
  1806. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1807. /* see ocfs2_file_aio_write */
  1808. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1809. rw_level = -1;
  1810. have_alloc_sem = 0;
  1811. }
  1812. bail:
  1813. if (have_alloc_sem)
  1814. up_read(&inode->i_alloc_sem);
  1815. if (rw_level != -1)
  1816. ocfs2_rw_unlock(inode, rw_level);
  1817. mlog_exit(ret);
  1818. return ret;
  1819. }
  1820. const struct inode_operations ocfs2_file_iops = {
  1821. .setattr = ocfs2_setattr,
  1822. .getattr = ocfs2_getattr,
  1823. .permission = ocfs2_permission,
  1824. .fallocate = ocfs2_fallocate,
  1825. };
  1826. const struct inode_operations ocfs2_special_file_iops = {
  1827. .setattr = ocfs2_setattr,
  1828. .getattr = ocfs2_getattr,
  1829. .permission = ocfs2_permission,
  1830. };
  1831. const struct file_operations ocfs2_fops = {
  1832. .read = do_sync_read,
  1833. .write = do_sync_write,
  1834. .mmap = ocfs2_mmap,
  1835. .fsync = ocfs2_sync_file,
  1836. .release = ocfs2_file_release,
  1837. .open = ocfs2_file_open,
  1838. .aio_read = ocfs2_file_aio_read,
  1839. .aio_write = ocfs2_file_aio_write,
  1840. .ioctl = ocfs2_ioctl,
  1841. #ifdef CONFIG_COMPAT
  1842. .compat_ioctl = ocfs2_compat_ioctl,
  1843. #endif
  1844. .splice_read = ocfs2_file_splice_read,
  1845. .splice_write = ocfs2_file_splice_write,
  1846. };
  1847. const struct file_operations ocfs2_dops = {
  1848. .read = generic_read_dir,
  1849. .readdir = ocfs2_readdir,
  1850. .fsync = ocfs2_sync_file,
  1851. .ioctl = ocfs2_ioctl,
  1852. #ifdef CONFIG_COMPAT
  1853. .compat_ioctl = ocfs2_compat_ioctl,
  1854. #endif
  1855. };