file.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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/pipe_fs_i.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #define MLOG_MASK_PREFIX ML_INODE
  37. #include <cluster/masklog.h>
  38. #include "ocfs2.h"
  39. #include "alloc.h"
  40. #include "aops.h"
  41. #include "dir.h"
  42. #include "dlmglue.h"
  43. #include "extent_map.h"
  44. #include "file.h"
  45. #include "sysfile.h"
  46. #include "inode.h"
  47. #include "ioctl.h"
  48. #include "journal.h"
  49. #include "mmap.h"
  50. #include "suballoc.h"
  51. #include "super.h"
  52. #include "buffer_head_io.h"
  53. static int ocfs2_sync_inode(struct inode *inode)
  54. {
  55. filemap_fdatawrite(inode->i_mapping);
  56. return sync_mapping_buffers(inode->i_mapping);
  57. }
  58. static int ocfs2_file_open(struct inode *inode, struct file *file)
  59. {
  60. int status;
  61. int mode = file->f_flags;
  62. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  63. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  64. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  65. spin_lock(&oi->ip_lock);
  66. /* Check that the inode hasn't been wiped from disk by another
  67. * node. If it hasn't then we're safe as long as we hold the
  68. * spin lock until our increment of open count. */
  69. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  70. spin_unlock(&oi->ip_lock);
  71. status = -ENOENT;
  72. goto leave;
  73. }
  74. if (mode & O_DIRECT)
  75. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  76. oi->ip_open_count++;
  77. spin_unlock(&oi->ip_lock);
  78. status = 0;
  79. leave:
  80. mlog_exit(status);
  81. return status;
  82. }
  83. static int ocfs2_file_release(struct inode *inode, struct file *file)
  84. {
  85. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  86. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  87. file->f_path.dentry->d_name.len,
  88. file->f_path.dentry->d_name.name);
  89. spin_lock(&oi->ip_lock);
  90. if (!--oi->ip_open_count)
  91. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  92. spin_unlock(&oi->ip_lock);
  93. mlog_exit(0);
  94. return 0;
  95. }
  96. static int ocfs2_sync_file(struct file *file,
  97. struct dentry *dentry,
  98. int datasync)
  99. {
  100. int err = 0;
  101. journal_t *journal;
  102. struct inode *inode = dentry->d_inode;
  103. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  104. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  105. dentry->d_name.len, dentry->d_name.name);
  106. err = ocfs2_sync_inode(dentry->d_inode);
  107. if (err)
  108. goto bail;
  109. journal = osb->journal->j_journal;
  110. err = journal_force_commit(journal);
  111. bail:
  112. mlog_exit(err);
  113. return (err < 0) ? -EIO : 0;
  114. }
  115. int ocfs2_should_update_atime(struct inode *inode,
  116. struct vfsmount *vfsmnt)
  117. {
  118. struct timespec now;
  119. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  120. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  121. return 0;
  122. if ((inode->i_flags & S_NOATIME) ||
  123. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  124. return 0;
  125. /*
  126. * We can be called with no vfsmnt structure - NFSD will
  127. * sometimes do this.
  128. *
  129. * Note that our action here is different than touch_atime() -
  130. * if we can't tell whether this is a noatime mount, then we
  131. * don't know whether to trust the value of s_atime_quantum.
  132. */
  133. if (vfsmnt == NULL)
  134. return 0;
  135. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  136. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  137. return 0;
  138. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  139. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  140. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  141. return 1;
  142. return 0;
  143. }
  144. now = CURRENT_TIME;
  145. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  146. return 0;
  147. else
  148. return 1;
  149. }
  150. int ocfs2_update_inode_atime(struct inode *inode,
  151. struct buffer_head *bh)
  152. {
  153. int ret;
  154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  155. handle_t *handle;
  156. mlog_entry_void();
  157. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  158. if (handle == NULL) {
  159. ret = -ENOMEM;
  160. mlog_errno(ret);
  161. goto out;
  162. }
  163. inode->i_atime = CURRENT_TIME;
  164. ret = ocfs2_mark_inode_dirty(handle, inode, bh);
  165. if (ret < 0)
  166. mlog_errno(ret);
  167. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  168. out:
  169. mlog_exit(ret);
  170. return ret;
  171. }
  172. static int ocfs2_set_inode_size(handle_t *handle,
  173. struct inode *inode,
  174. struct buffer_head *fe_bh,
  175. u64 new_i_size)
  176. {
  177. int status;
  178. mlog_entry_void();
  179. i_size_write(inode, new_i_size);
  180. inode->i_blocks = ocfs2_inode_sector_count(inode);
  181. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  182. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  183. if (status < 0) {
  184. mlog_errno(status);
  185. goto bail;
  186. }
  187. bail:
  188. mlog_exit(status);
  189. return status;
  190. }
  191. static int ocfs2_simple_size_update(struct inode *inode,
  192. struct buffer_head *di_bh,
  193. u64 new_i_size)
  194. {
  195. int ret;
  196. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  197. handle_t *handle = NULL;
  198. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  199. if (handle == NULL) {
  200. ret = -ENOMEM;
  201. mlog_errno(ret);
  202. goto out;
  203. }
  204. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  205. new_i_size);
  206. if (ret < 0)
  207. mlog_errno(ret);
  208. ocfs2_commit_trans(osb, handle);
  209. out:
  210. return ret;
  211. }
  212. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  213. struct inode *inode,
  214. struct buffer_head *fe_bh,
  215. u64 new_i_size)
  216. {
  217. int status;
  218. handle_t *handle;
  219. struct ocfs2_dinode *di;
  220. mlog_entry_void();
  221. /* TODO: This needs to actually orphan the inode in this
  222. * transaction. */
  223. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  224. if (IS_ERR(handle)) {
  225. status = PTR_ERR(handle);
  226. mlog_errno(status);
  227. goto out;
  228. }
  229. status = ocfs2_journal_access(handle, inode, fe_bh,
  230. OCFS2_JOURNAL_ACCESS_WRITE);
  231. if (status < 0) {
  232. mlog_errno(status);
  233. goto out_commit;
  234. }
  235. /*
  236. * Do this before setting i_size.
  237. */
  238. status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
  239. if (status) {
  240. mlog_errno(status);
  241. goto out_commit;
  242. }
  243. i_size_write(inode, new_i_size);
  244. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  245. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  246. di = (struct ocfs2_dinode *) fe_bh->b_data;
  247. di->i_size = cpu_to_le64(new_i_size);
  248. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  249. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  250. status = ocfs2_journal_dirty(handle, fe_bh);
  251. if (status < 0)
  252. mlog_errno(status);
  253. out_commit:
  254. ocfs2_commit_trans(osb, handle);
  255. out:
  256. mlog_exit(status);
  257. return status;
  258. }
  259. static int ocfs2_truncate_file(struct inode *inode,
  260. struct buffer_head *di_bh,
  261. u64 new_i_size)
  262. {
  263. int status = 0;
  264. struct ocfs2_dinode *fe = NULL;
  265. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  266. struct ocfs2_truncate_context *tc = NULL;
  267. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  268. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  269. (unsigned long long)new_i_size);
  270. truncate_inode_pages(inode->i_mapping, new_i_size);
  271. fe = (struct ocfs2_dinode *) di_bh->b_data;
  272. if (!OCFS2_IS_VALID_DINODE(fe)) {
  273. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  274. status = -EIO;
  275. goto bail;
  276. }
  277. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  278. "Inode %llu, inode i_size = %lld != di "
  279. "i_size = %llu, i_flags = 0x%x\n",
  280. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  281. i_size_read(inode),
  282. (unsigned long long)le64_to_cpu(fe->i_size),
  283. le32_to_cpu(fe->i_flags));
  284. if (new_i_size > le64_to_cpu(fe->i_size)) {
  285. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  286. (unsigned long long)le64_to_cpu(fe->i_size),
  287. (unsigned long long)new_i_size);
  288. status = -EINVAL;
  289. mlog_errno(status);
  290. goto bail;
  291. }
  292. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  293. (unsigned long long)le64_to_cpu(fe->i_blkno),
  294. (unsigned long long)le64_to_cpu(fe->i_size),
  295. (unsigned long long)new_i_size);
  296. /* lets handle the simple truncate cases before doing any more
  297. * cluster locking. */
  298. if (new_i_size == le64_to_cpu(fe->i_size))
  299. goto bail;
  300. /* This forces other nodes to sync and drop their pages. Do
  301. * this even if we have a truncate without allocation change -
  302. * ocfs2 cluster sizes can be much greater than page size, so
  303. * we have to truncate them anyway. */
  304. status = ocfs2_data_lock(inode, 1);
  305. if (status < 0) {
  306. mlog_errno(status);
  307. goto bail;
  308. }
  309. /* alright, we're going to need to do a full blown alloc size
  310. * change. Orphan the inode so that recovery can complete the
  311. * truncate if necessary. This does the task of marking
  312. * i_size. */
  313. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  314. if (status < 0) {
  315. mlog_errno(status);
  316. goto bail_unlock_data;
  317. }
  318. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  319. if (status < 0) {
  320. mlog_errno(status);
  321. goto bail_unlock_data;
  322. }
  323. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  324. if (status < 0) {
  325. mlog_errno(status);
  326. goto bail_unlock_data;
  327. }
  328. /* TODO: orphan dir cleanup here. */
  329. bail_unlock_data:
  330. ocfs2_data_unlock(inode, 1);
  331. bail:
  332. mlog_exit(status);
  333. return status;
  334. }
  335. /*
  336. * extend allocation only here.
  337. * we'll update all the disk stuff, and oip->alloc_size
  338. *
  339. * expect stuff to be locked, a transaction started and enough data /
  340. * metadata reservations in the contexts.
  341. *
  342. * Will return -EAGAIN, and a reason if a restart is needed.
  343. * If passed in, *reason will always be set, even in error.
  344. */
  345. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  346. struct inode *inode,
  347. u32 *logical_offset,
  348. u32 clusters_to_add,
  349. struct buffer_head *fe_bh,
  350. handle_t *handle,
  351. struct ocfs2_alloc_context *data_ac,
  352. struct ocfs2_alloc_context *meta_ac,
  353. enum ocfs2_alloc_restarted *reason_ret)
  354. {
  355. int status = 0;
  356. int free_extents;
  357. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  358. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  359. u32 bit_off, num_bits;
  360. u64 block;
  361. BUG_ON(!clusters_to_add);
  362. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  363. if (free_extents < 0) {
  364. status = free_extents;
  365. mlog_errno(status);
  366. goto leave;
  367. }
  368. /* there are two cases which could cause us to EAGAIN in the
  369. * we-need-more-metadata case:
  370. * 1) we haven't reserved *any*
  371. * 2) we are so fragmented, we've needed to add metadata too
  372. * many times. */
  373. if (!free_extents && !meta_ac) {
  374. mlog(0, "we haven't reserved any metadata!\n");
  375. status = -EAGAIN;
  376. reason = RESTART_META;
  377. goto leave;
  378. } else if ((!free_extents)
  379. && (ocfs2_alloc_context_bits_left(meta_ac)
  380. < ocfs2_extend_meta_needed(fe))) {
  381. mlog(0, "filesystem is really fragmented...\n");
  382. status = -EAGAIN;
  383. reason = RESTART_META;
  384. goto leave;
  385. }
  386. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  387. &bit_off, &num_bits);
  388. if (status < 0) {
  389. if (status != -ENOSPC)
  390. mlog_errno(status);
  391. goto leave;
  392. }
  393. BUG_ON(num_bits > clusters_to_add);
  394. /* reserve our write early -- insert_extent may update the inode */
  395. status = ocfs2_journal_access(handle, inode, fe_bh,
  396. OCFS2_JOURNAL_ACCESS_WRITE);
  397. if (status < 0) {
  398. mlog_errno(status);
  399. goto leave;
  400. }
  401. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  402. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  403. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  404. status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
  405. *logical_offset, block, num_bits,
  406. meta_ac);
  407. if (status < 0) {
  408. mlog_errno(status);
  409. goto leave;
  410. }
  411. status = ocfs2_journal_dirty(handle, fe_bh);
  412. if (status < 0) {
  413. mlog_errno(status);
  414. goto leave;
  415. }
  416. clusters_to_add -= num_bits;
  417. *logical_offset += num_bits;
  418. if (clusters_to_add) {
  419. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  420. "%u\n", fe->i_clusters, clusters_to_add);
  421. status = -EAGAIN;
  422. reason = RESTART_TRANS;
  423. }
  424. leave:
  425. mlog_exit(status);
  426. if (reason_ret)
  427. *reason_ret = reason;
  428. return status;
  429. }
  430. /*
  431. * For a given allocation, determine which allocators will need to be
  432. * accessed, and lock them, reserving the appropriate number of bits.
  433. *
  434. * Called from ocfs2_extend_allocation() for file systems which don't
  435. * support holes, and from ocfs2_write() for file systems which
  436. * understand sparse inodes.
  437. */
  438. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
  439. u32 clusters_to_add,
  440. struct ocfs2_alloc_context **data_ac,
  441. struct ocfs2_alloc_context **meta_ac)
  442. {
  443. int ret, num_free_extents;
  444. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  445. *meta_ac = NULL;
  446. *data_ac = NULL;
  447. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  448. "clusters_to_add = %u\n",
  449. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  450. le32_to_cpu(di->i_clusters), clusters_to_add);
  451. num_free_extents = ocfs2_num_free_extents(osb, inode, di);
  452. if (num_free_extents < 0) {
  453. ret = num_free_extents;
  454. mlog_errno(ret);
  455. goto out;
  456. }
  457. /*
  458. * Sparse allocation file systems need to be more conservative
  459. * with reserving room for expansion - the actual allocation
  460. * happens while we've got a journal handle open so re-taking
  461. * a cluster lock (because we ran out of room for another
  462. * extent) will violate ordering rules.
  463. *
  464. * Most of the time we'll only be seeing this 1 cluster at a time
  465. * anyway.
  466. */
  467. if (!num_free_extents ||
  468. (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
  469. ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
  470. if (ret < 0) {
  471. if (ret != -ENOSPC)
  472. mlog_errno(ret);
  473. goto out;
  474. }
  475. }
  476. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  477. if (ret < 0) {
  478. if (ret != -ENOSPC)
  479. mlog_errno(ret);
  480. goto out;
  481. }
  482. out:
  483. if (ret) {
  484. if (*meta_ac) {
  485. ocfs2_free_alloc_context(*meta_ac);
  486. *meta_ac = NULL;
  487. }
  488. /*
  489. * We cannot have an error and a non null *data_ac.
  490. */
  491. }
  492. return ret;
  493. }
  494. static int ocfs2_extend_allocation(struct inode *inode,
  495. u32 clusters_to_add)
  496. {
  497. int status = 0;
  498. int restart_func = 0;
  499. int drop_alloc_sem = 0;
  500. int credits;
  501. u32 prev_clusters, logical_start;
  502. struct buffer_head *bh = NULL;
  503. struct ocfs2_dinode *fe = NULL;
  504. handle_t *handle = NULL;
  505. struct ocfs2_alloc_context *data_ac = NULL;
  506. struct ocfs2_alloc_context *meta_ac = NULL;
  507. enum ocfs2_alloc_restarted why;
  508. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  509. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  510. /*
  511. * This function only exists for file systems which don't
  512. * support holes.
  513. */
  514. BUG_ON(ocfs2_sparse_alloc(osb));
  515. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  516. OCFS2_BH_CACHED, inode);
  517. if (status < 0) {
  518. mlog_errno(status);
  519. goto leave;
  520. }
  521. fe = (struct ocfs2_dinode *) bh->b_data;
  522. if (!OCFS2_IS_VALID_DINODE(fe)) {
  523. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  524. status = -EIO;
  525. goto leave;
  526. }
  527. logical_start = OCFS2_I(inode)->ip_clusters;
  528. restart_all:
  529. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  530. /* blocks peope in read/write from reading our allocation
  531. * until we're done changing it. We depend on i_mutex to block
  532. * other extend/truncate calls while we're here. Ordering wrt
  533. * start_trans is important here -- always do it before! */
  534. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  535. drop_alloc_sem = 1;
  536. status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
  537. &meta_ac);
  538. if (status) {
  539. mlog_errno(status);
  540. goto leave;
  541. }
  542. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  543. handle = ocfs2_start_trans(osb, credits);
  544. if (IS_ERR(handle)) {
  545. status = PTR_ERR(handle);
  546. handle = NULL;
  547. mlog_errno(status);
  548. goto leave;
  549. }
  550. restarted_transaction:
  551. /* reserve a write to the file entry early on - that we if we
  552. * run out of credits in the allocation path, we can still
  553. * update i_size. */
  554. status = ocfs2_journal_access(handle, inode, bh,
  555. OCFS2_JOURNAL_ACCESS_WRITE);
  556. if (status < 0) {
  557. mlog_errno(status);
  558. goto leave;
  559. }
  560. prev_clusters = OCFS2_I(inode)->ip_clusters;
  561. status = ocfs2_do_extend_allocation(osb,
  562. inode,
  563. &logical_start,
  564. clusters_to_add,
  565. bh,
  566. handle,
  567. data_ac,
  568. meta_ac,
  569. &why);
  570. if ((status < 0) && (status != -EAGAIN)) {
  571. if (status != -ENOSPC)
  572. mlog_errno(status);
  573. goto leave;
  574. }
  575. status = ocfs2_journal_dirty(handle, bh);
  576. if (status < 0) {
  577. mlog_errno(status);
  578. goto leave;
  579. }
  580. spin_lock(&OCFS2_I(inode)->ip_lock);
  581. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  582. spin_unlock(&OCFS2_I(inode)->ip_lock);
  583. if (why != RESTART_NONE && clusters_to_add) {
  584. if (why == RESTART_META) {
  585. mlog(0, "restarting function.\n");
  586. restart_func = 1;
  587. } else {
  588. BUG_ON(why != RESTART_TRANS);
  589. mlog(0, "restarting transaction.\n");
  590. /* TODO: This can be more intelligent. */
  591. credits = ocfs2_calc_extend_credits(osb->sb,
  592. fe,
  593. clusters_to_add);
  594. status = ocfs2_extend_trans(handle, credits);
  595. if (status < 0) {
  596. /* handle still has to be committed at
  597. * this point. */
  598. status = -ENOMEM;
  599. mlog_errno(status);
  600. goto leave;
  601. }
  602. goto restarted_transaction;
  603. }
  604. }
  605. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  606. le32_to_cpu(fe->i_clusters),
  607. (unsigned long long)le64_to_cpu(fe->i_size));
  608. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  609. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  610. leave:
  611. if (drop_alloc_sem) {
  612. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  613. drop_alloc_sem = 0;
  614. }
  615. if (handle) {
  616. ocfs2_commit_trans(osb, handle);
  617. handle = NULL;
  618. }
  619. if (data_ac) {
  620. ocfs2_free_alloc_context(data_ac);
  621. data_ac = NULL;
  622. }
  623. if (meta_ac) {
  624. ocfs2_free_alloc_context(meta_ac);
  625. meta_ac = NULL;
  626. }
  627. if ((!status) && restart_func) {
  628. restart_func = 0;
  629. goto restart_all;
  630. }
  631. if (bh) {
  632. brelse(bh);
  633. bh = NULL;
  634. }
  635. mlog_exit(status);
  636. return status;
  637. }
  638. /* Some parts of this taken from generic_cont_expand, which turned out
  639. * to be too fragile to do exactly what we need without us having to
  640. * worry about recursive locking in ->prepare_write() and
  641. * ->commit_write(). */
  642. static int ocfs2_write_zero_page(struct inode *inode,
  643. u64 size)
  644. {
  645. struct address_space *mapping = inode->i_mapping;
  646. struct page *page;
  647. unsigned long index;
  648. unsigned int offset;
  649. handle_t *handle = NULL;
  650. int ret;
  651. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  652. /* ugh. in prepare/commit_write, if from==to==start of block, we
  653. ** skip the prepare. make sure we never send an offset for the start
  654. ** of a block
  655. */
  656. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  657. offset++;
  658. }
  659. index = size >> PAGE_CACHE_SHIFT;
  660. page = grab_cache_page(mapping, index);
  661. if (!page) {
  662. ret = -ENOMEM;
  663. mlog_errno(ret);
  664. goto out;
  665. }
  666. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  667. if (ret < 0) {
  668. mlog_errno(ret);
  669. goto out_unlock;
  670. }
  671. if (ocfs2_should_order_data(inode)) {
  672. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  673. offset);
  674. if (IS_ERR(handle)) {
  675. ret = PTR_ERR(handle);
  676. handle = NULL;
  677. goto out_unlock;
  678. }
  679. }
  680. /* must not update i_size! */
  681. ret = block_commit_write(page, offset, offset);
  682. if (ret < 0)
  683. mlog_errno(ret);
  684. else
  685. ret = 0;
  686. if (handle)
  687. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  688. out_unlock:
  689. unlock_page(page);
  690. page_cache_release(page);
  691. out:
  692. return ret;
  693. }
  694. static int ocfs2_zero_extend(struct inode *inode,
  695. u64 zero_to_size)
  696. {
  697. int ret = 0;
  698. u64 start_off;
  699. struct super_block *sb = inode->i_sb;
  700. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  701. while (start_off < zero_to_size) {
  702. ret = ocfs2_write_zero_page(inode, start_off);
  703. if (ret < 0) {
  704. mlog_errno(ret);
  705. goto out;
  706. }
  707. start_off += sb->s_blocksize;
  708. /*
  709. * Very large extends have the potential to lock up
  710. * the cpu for extended periods of time.
  711. */
  712. cond_resched();
  713. }
  714. out:
  715. return ret;
  716. }
  717. /*
  718. * A tail_to_skip value > 0 indicates that we're being called from
  719. * ocfs2_file_aio_write(). This has the following implications:
  720. *
  721. * - we don't want to update i_size
  722. * - di_bh will be NULL, which is fine because it's only used in the
  723. * case where we want to update i_size.
  724. * - ocfs2_zero_extend() will then only be filling the hole created
  725. * between i_size and the start of the write.
  726. */
  727. static int ocfs2_extend_file(struct inode *inode,
  728. struct buffer_head *di_bh,
  729. u64 new_i_size,
  730. size_t tail_to_skip)
  731. {
  732. int ret = 0;
  733. u32 clusters_to_add = 0;
  734. BUG_ON(!tail_to_skip && !di_bh);
  735. /* setattr sometimes calls us like this. */
  736. if (new_i_size == 0)
  737. goto out;
  738. if (i_size_read(inode) == new_i_size)
  739. goto out;
  740. BUG_ON(new_i_size < i_size_read(inode));
  741. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  742. BUG_ON(tail_to_skip != 0);
  743. goto out_update_size;
  744. }
  745. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  746. OCFS2_I(inode)->ip_clusters;
  747. /*
  748. * protect the pages that ocfs2_zero_extend is going to be
  749. * pulling into the page cache.. we do this before the
  750. * metadata extend so that we don't get into the situation
  751. * where we've extended the metadata but can't get the data
  752. * lock to zero.
  753. */
  754. ret = ocfs2_data_lock(inode, 1);
  755. if (ret < 0) {
  756. mlog_errno(ret);
  757. goto out;
  758. }
  759. if (clusters_to_add) {
  760. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  761. if (ret < 0) {
  762. mlog_errno(ret);
  763. goto out_unlock;
  764. }
  765. }
  766. /*
  767. * Call this even if we don't add any clusters to the tree. We
  768. * still need to zero the area between the old i_size and the
  769. * new i_size.
  770. */
  771. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  772. if (ret < 0) {
  773. mlog_errno(ret);
  774. goto out_unlock;
  775. }
  776. out_update_size:
  777. if (!tail_to_skip) {
  778. /* We're being called from ocfs2_setattr() which wants
  779. * us to update i_size */
  780. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  781. if (ret < 0)
  782. mlog_errno(ret);
  783. }
  784. out_unlock:
  785. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  786. ocfs2_data_unlock(inode, 1);
  787. out:
  788. return ret;
  789. }
  790. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  791. {
  792. int status = 0, size_change;
  793. struct inode *inode = dentry->d_inode;
  794. struct super_block *sb = inode->i_sb;
  795. struct ocfs2_super *osb = OCFS2_SB(sb);
  796. struct buffer_head *bh = NULL;
  797. handle_t *handle = NULL;
  798. mlog_entry("(0x%p, '%.*s')\n", dentry,
  799. dentry->d_name.len, dentry->d_name.name);
  800. if (attr->ia_valid & ATTR_MODE)
  801. mlog(0, "mode change: %d\n", attr->ia_mode);
  802. if (attr->ia_valid & ATTR_UID)
  803. mlog(0, "uid change: %d\n", attr->ia_uid);
  804. if (attr->ia_valid & ATTR_GID)
  805. mlog(0, "gid change: %d\n", attr->ia_gid);
  806. if (attr->ia_valid & ATTR_SIZE)
  807. mlog(0, "size change...\n");
  808. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  809. mlog(0, "time change...\n");
  810. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  811. | ATTR_GID | ATTR_UID | ATTR_MODE)
  812. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  813. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  814. return 0;
  815. }
  816. status = inode_change_ok(inode, attr);
  817. if (status)
  818. return status;
  819. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  820. if (size_change) {
  821. status = ocfs2_rw_lock(inode, 1);
  822. if (status < 0) {
  823. mlog_errno(status);
  824. goto bail;
  825. }
  826. }
  827. status = ocfs2_meta_lock(inode, &bh, 1);
  828. if (status < 0) {
  829. if (status != -ENOENT)
  830. mlog_errno(status);
  831. goto bail_unlock_rw;
  832. }
  833. if (size_change && attr->ia_size != i_size_read(inode)) {
  834. if (i_size_read(inode) > attr->ia_size)
  835. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  836. else
  837. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  838. if (status < 0) {
  839. if (status != -ENOSPC)
  840. mlog_errno(status);
  841. status = -ENOSPC;
  842. goto bail_unlock;
  843. }
  844. }
  845. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  846. if (IS_ERR(handle)) {
  847. status = PTR_ERR(handle);
  848. mlog_errno(status);
  849. goto bail_unlock;
  850. }
  851. status = inode_setattr(inode, attr);
  852. if (status < 0) {
  853. mlog_errno(status);
  854. goto bail_commit;
  855. }
  856. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  857. if (status < 0)
  858. mlog_errno(status);
  859. bail_commit:
  860. ocfs2_commit_trans(osb, handle);
  861. bail_unlock:
  862. ocfs2_meta_unlock(inode, 1);
  863. bail_unlock_rw:
  864. if (size_change)
  865. ocfs2_rw_unlock(inode, 1);
  866. bail:
  867. if (bh)
  868. brelse(bh);
  869. mlog_exit(status);
  870. return status;
  871. }
  872. int ocfs2_getattr(struct vfsmount *mnt,
  873. struct dentry *dentry,
  874. struct kstat *stat)
  875. {
  876. struct inode *inode = dentry->d_inode;
  877. struct super_block *sb = dentry->d_inode->i_sb;
  878. struct ocfs2_super *osb = sb->s_fs_info;
  879. int err;
  880. mlog_entry_void();
  881. err = ocfs2_inode_revalidate(dentry);
  882. if (err) {
  883. if (err != -ENOENT)
  884. mlog_errno(err);
  885. goto bail;
  886. }
  887. generic_fillattr(inode, stat);
  888. /* We set the blksize from the cluster size for performance */
  889. stat->blksize = osb->s_clustersize;
  890. bail:
  891. mlog_exit(err);
  892. return err;
  893. }
  894. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  895. {
  896. int ret;
  897. mlog_entry_void();
  898. ret = ocfs2_meta_lock(inode, NULL, 0);
  899. if (ret) {
  900. if (ret != -ENOENT)
  901. mlog_errno(ret);
  902. goto out;
  903. }
  904. ret = generic_permission(inode, mask, NULL);
  905. ocfs2_meta_unlock(inode, 0);
  906. out:
  907. mlog_exit(ret);
  908. return ret;
  909. }
  910. static int ocfs2_write_remove_suid(struct inode *inode)
  911. {
  912. int ret;
  913. struct buffer_head *bh = NULL;
  914. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  915. handle_t *handle;
  916. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  917. struct ocfs2_dinode *di;
  918. mlog_entry("(Inode %llu, mode 0%o)\n",
  919. (unsigned long long)oi->ip_blkno, inode->i_mode);
  920. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  921. if (handle == NULL) {
  922. ret = -ENOMEM;
  923. mlog_errno(ret);
  924. goto out;
  925. }
  926. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  927. if (ret < 0) {
  928. mlog_errno(ret);
  929. goto out_trans;
  930. }
  931. ret = ocfs2_journal_access(handle, inode, bh,
  932. OCFS2_JOURNAL_ACCESS_WRITE);
  933. if (ret < 0) {
  934. mlog_errno(ret);
  935. goto out_bh;
  936. }
  937. inode->i_mode &= ~S_ISUID;
  938. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  939. inode->i_mode &= ~S_ISGID;
  940. di = (struct ocfs2_dinode *) bh->b_data;
  941. di->i_mode = cpu_to_le16(inode->i_mode);
  942. ret = ocfs2_journal_dirty(handle, bh);
  943. if (ret < 0)
  944. mlog_errno(ret);
  945. out_bh:
  946. brelse(bh);
  947. out_trans:
  948. ocfs2_commit_trans(osb, handle);
  949. out:
  950. mlog_exit(ret);
  951. return ret;
  952. }
  953. /*
  954. * Will look for holes and unwritten extents in the range starting at
  955. * pos for count bytes (inclusive).
  956. */
  957. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  958. size_t count)
  959. {
  960. int ret = 0;
  961. unsigned int extent_flags;
  962. u32 cpos, clusters, extent_len, phys_cpos;
  963. struct super_block *sb = inode->i_sb;
  964. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  965. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  966. while (clusters) {
  967. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  968. &extent_flags);
  969. if (ret < 0) {
  970. mlog_errno(ret);
  971. goto out;
  972. }
  973. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  974. ret = 1;
  975. break;
  976. }
  977. if (extent_len > clusters)
  978. extent_len = clusters;
  979. clusters -= extent_len;
  980. cpos += extent_len;
  981. }
  982. out:
  983. return ret;
  984. }
  985. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  986. loff_t *ppos,
  987. size_t count,
  988. int appending,
  989. int *direct_io)
  990. {
  991. int ret = 0, meta_level = appending;
  992. struct inode *inode = dentry->d_inode;
  993. u32 clusters;
  994. loff_t newsize, saved_pos;
  995. /*
  996. * We sample i_size under a read level meta lock to see if our write
  997. * is extending the file, if it is we back off and get a write level
  998. * meta lock.
  999. */
  1000. for(;;) {
  1001. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1002. if (ret < 0) {
  1003. meta_level = -1;
  1004. mlog_errno(ret);
  1005. goto out;
  1006. }
  1007. /* Clear suid / sgid if necessary. We do this here
  1008. * instead of later in the write path because
  1009. * remove_suid() calls ->setattr without any hint that
  1010. * we may have already done our cluster locking. Since
  1011. * ocfs2_setattr() *must* take cluster locks to
  1012. * proceeed, this will lead us to recursively lock the
  1013. * inode. There's also the dinode i_size state which
  1014. * can be lost via setattr during extending writes (we
  1015. * set inode->i_size at the end of a write. */
  1016. if (should_remove_suid(dentry)) {
  1017. if (meta_level == 0) {
  1018. ocfs2_meta_unlock(inode, meta_level);
  1019. meta_level = 1;
  1020. continue;
  1021. }
  1022. ret = ocfs2_write_remove_suid(inode);
  1023. if (ret < 0) {
  1024. mlog_errno(ret);
  1025. goto out_unlock;
  1026. }
  1027. }
  1028. /* work on a copy of ppos until we're sure that we won't have
  1029. * to recalculate it due to relocking. */
  1030. if (appending) {
  1031. saved_pos = i_size_read(inode);
  1032. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1033. } else {
  1034. saved_pos = *ppos;
  1035. }
  1036. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  1037. loff_t end = saved_pos + count;
  1038. /*
  1039. * Skip the O_DIRECT checks if we don't need
  1040. * them.
  1041. */
  1042. if (!direct_io || !(*direct_io))
  1043. break;
  1044. /*
  1045. * Allowing concurrent direct writes means
  1046. * i_size changes wouldn't be synchronized, so
  1047. * one node could wind up truncating another
  1048. * nodes writes.
  1049. */
  1050. if (end > i_size_read(inode)) {
  1051. *direct_io = 0;
  1052. break;
  1053. }
  1054. /*
  1055. * We don't fill holes during direct io, so
  1056. * check for them here. If any are found, the
  1057. * caller will have to retake some cluster
  1058. * locks and initiate the io as buffered.
  1059. */
  1060. ret = ocfs2_check_range_for_holes(inode, saved_pos,
  1061. count);
  1062. if (ret == 1) {
  1063. *direct_io = 0;
  1064. ret = 0;
  1065. } else if (ret < 0)
  1066. mlog_errno(ret);
  1067. break;
  1068. }
  1069. /*
  1070. * The rest of this loop is concerned with legacy file
  1071. * systems which don't support sparse files.
  1072. */
  1073. newsize = count + saved_pos;
  1074. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  1075. (long long) saved_pos, (long long) newsize,
  1076. (long long) i_size_read(inode));
  1077. /* No need for a higher level metadata lock if we're
  1078. * never going past i_size. */
  1079. if (newsize <= i_size_read(inode))
  1080. break;
  1081. if (meta_level == 0) {
  1082. ocfs2_meta_unlock(inode, meta_level);
  1083. meta_level = 1;
  1084. continue;
  1085. }
  1086. spin_lock(&OCFS2_I(inode)->ip_lock);
  1087. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  1088. OCFS2_I(inode)->ip_clusters;
  1089. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1090. mlog(0, "Writing at EOF, may need more allocation: "
  1091. "i_size = %lld, newsize = %lld, need %u clusters\n",
  1092. (long long) i_size_read(inode), (long long) newsize,
  1093. clusters);
  1094. /* We only want to continue the rest of this loop if
  1095. * our extend will actually require more
  1096. * allocation. */
  1097. if (!clusters)
  1098. break;
  1099. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  1100. if (ret < 0) {
  1101. if (ret != -ENOSPC)
  1102. mlog_errno(ret);
  1103. goto out_unlock;
  1104. }
  1105. break;
  1106. }
  1107. if (appending)
  1108. *ppos = saved_pos;
  1109. out_unlock:
  1110. ocfs2_meta_unlock(inode, meta_level);
  1111. out:
  1112. return ret;
  1113. }
  1114. static inline void
  1115. ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  1116. {
  1117. const struct iovec *iov = *iovp;
  1118. size_t base = *basep;
  1119. do {
  1120. int copy = min(bytes, iov->iov_len - base);
  1121. bytes -= copy;
  1122. base += copy;
  1123. if (iov->iov_len == base) {
  1124. iov++;
  1125. base = 0;
  1126. }
  1127. } while (bytes);
  1128. *iovp = iov;
  1129. *basep = base;
  1130. }
  1131. static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp,
  1132. const struct iovec *cur_iov,
  1133. size_t iov_offset)
  1134. {
  1135. int ret;
  1136. char *buf;
  1137. struct page *src_page = NULL;
  1138. buf = cur_iov->iov_base + iov_offset;
  1139. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1140. /*
  1141. * Pull in the user page. We want to do this outside
  1142. * of the meta data locks in order to preserve locking
  1143. * order in case of page fault.
  1144. */
  1145. ret = get_user_pages(current, current->mm,
  1146. (unsigned long)buf & PAGE_CACHE_MASK, 1,
  1147. 0, 0, &src_page, NULL);
  1148. if (ret == 1)
  1149. bp->b_src_buf = kmap(src_page);
  1150. else
  1151. src_page = ERR_PTR(-EFAULT);
  1152. } else {
  1153. bp->b_src_buf = buf;
  1154. }
  1155. return src_page;
  1156. }
  1157. static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp,
  1158. struct page *page)
  1159. {
  1160. if (page) {
  1161. kunmap(page);
  1162. page_cache_release(page);
  1163. }
  1164. }
  1165. static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
  1166. const struct iovec *iov,
  1167. unsigned long nr_segs,
  1168. size_t count,
  1169. ssize_t o_direct_written)
  1170. {
  1171. int ret = 0;
  1172. ssize_t copied, total = 0;
  1173. size_t iov_offset = 0;
  1174. const struct iovec *cur_iov = iov;
  1175. struct ocfs2_buffered_write_priv bp;
  1176. struct page *page;
  1177. /*
  1178. * handle partial DIO write. Adjust cur_iov if needed.
  1179. */
  1180. ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
  1181. do {
  1182. bp.b_cur_off = iov_offset;
  1183. bp.b_cur_iov = cur_iov;
  1184. page = ocfs2_get_write_source(&bp, cur_iov, iov_offset);
  1185. if (IS_ERR(page)) {
  1186. ret = PTR_ERR(page);
  1187. goto out;
  1188. }
  1189. copied = ocfs2_buffered_write_cluster(file, *ppos, count,
  1190. ocfs2_map_and_write_user_data,
  1191. &bp);
  1192. ocfs2_put_write_source(&bp, page);
  1193. if (copied < 0) {
  1194. mlog_errno(copied);
  1195. ret = copied;
  1196. goto out;
  1197. }
  1198. total += copied;
  1199. *ppos = *ppos + copied;
  1200. count -= copied;
  1201. ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
  1202. } while(count);
  1203. out:
  1204. return total ? total : ret;
  1205. }
  1206. static int ocfs2_check_iovec(const struct iovec *iov, size_t *counted,
  1207. unsigned long *nr_segs)
  1208. {
  1209. size_t ocount; /* original count */
  1210. unsigned long seg;
  1211. ocount = 0;
  1212. for (seg = 0; seg < *nr_segs; seg++) {
  1213. const struct iovec *iv = &iov[seg];
  1214. /*
  1215. * If any segment has a negative length, or the cumulative
  1216. * length ever wraps negative then return -EINVAL.
  1217. */
  1218. ocount += iv->iov_len;
  1219. if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
  1220. return -EINVAL;
  1221. if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
  1222. continue;
  1223. if (seg == 0)
  1224. return -EFAULT;
  1225. *nr_segs = seg;
  1226. ocount -= iv->iov_len; /* This segment is no good */
  1227. break;
  1228. }
  1229. *counted = ocount;
  1230. return 0;
  1231. }
  1232. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1233. const struct iovec *iov,
  1234. unsigned long nr_segs,
  1235. loff_t pos)
  1236. {
  1237. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1238. int can_do_direct, sync = 0;
  1239. ssize_t written = 0;
  1240. size_t ocount; /* original count */
  1241. size_t count; /* after file limit checks */
  1242. loff_t *ppos = &iocb->ki_pos;
  1243. struct file *file = iocb->ki_filp;
  1244. struct inode *inode = file->f_path.dentry->d_inode;
  1245. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1246. (unsigned int)nr_segs,
  1247. file->f_path.dentry->d_name.len,
  1248. file->f_path.dentry->d_name.name);
  1249. if (iocb->ki_left == 0)
  1250. return 0;
  1251. ret = ocfs2_check_iovec(iov, &ocount, &nr_segs);
  1252. if (ret)
  1253. return ret;
  1254. count = ocount;
  1255. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1256. appending = file->f_flags & O_APPEND ? 1 : 0;
  1257. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1258. mutex_lock(&inode->i_mutex);
  1259. relock:
  1260. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1261. if (direct_io) {
  1262. down_read(&inode->i_alloc_sem);
  1263. have_alloc_sem = 1;
  1264. }
  1265. /* concurrent O_DIRECT writes are allowed */
  1266. rw_level = !direct_io;
  1267. ret = ocfs2_rw_lock(inode, rw_level);
  1268. if (ret < 0) {
  1269. mlog_errno(ret);
  1270. goto out_sems;
  1271. }
  1272. can_do_direct = direct_io;
  1273. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1274. iocb->ki_left, appending,
  1275. &can_do_direct);
  1276. if (ret < 0) {
  1277. mlog_errno(ret);
  1278. goto out;
  1279. }
  1280. /*
  1281. * We can't complete the direct I/O as requested, fall back to
  1282. * buffered I/O.
  1283. */
  1284. if (direct_io && !can_do_direct) {
  1285. ocfs2_rw_unlock(inode, rw_level);
  1286. up_read(&inode->i_alloc_sem);
  1287. have_alloc_sem = 0;
  1288. rw_level = -1;
  1289. direct_io = 0;
  1290. sync = 1;
  1291. goto relock;
  1292. }
  1293. if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
  1294. sync = 1;
  1295. /*
  1296. * XXX: Is it ok to execute these checks a second time?
  1297. */
  1298. ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
  1299. if (ret)
  1300. goto out;
  1301. /*
  1302. * Set pos so that sync_page_range_nolock() below understands
  1303. * where to start from. We might've moved it around via the
  1304. * calls above. The range we want to actually sync starts from
  1305. * *ppos here.
  1306. *
  1307. */
  1308. pos = *ppos;
  1309. /* communicate with ocfs2_dio_end_io */
  1310. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1311. if (direct_io) {
  1312. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1313. ppos, count, ocount);
  1314. if (written < 0) {
  1315. ret = written;
  1316. goto out_dio;
  1317. }
  1318. } else {
  1319. written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
  1320. count, written);
  1321. if (written < 0) {
  1322. ret = written;
  1323. if (ret != -EFAULT || ret != -ENOSPC)
  1324. mlog_errno(ret);
  1325. goto out;
  1326. }
  1327. }
  1328. out_dio:
  1329. /* buffered aio wouldn't have proper lock coverage today */
  1330. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1331. /*
  1332. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1333. * function pointer which is called when o_direct io completes so that
  1334. * it can unlock our rw lock. (it's the clustered equivalent of
  1335. * i_alloc_sem; protects truncate from racing with pending ios).
  1336. * Unfortunately there are error cases which call end_io and others
  1337. * that don't. so we don't have to unlock the rw_lock if either an
  1338. * async dio is going to do it in the future or an end_io after an
  1339. * error has already done it.
  1340. */
  1341. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1342. rw_level = -1;
  1343. have_alloc_sem = 0;
  1344. }
  1345. out:
  1346. if (rw_level != -1)
  1347. ocfs2_rw_unlock(inode, rw_level);
  1348. out_sems:
  1349. if (have_alloc_sem)
  1350. up_read(&inode->i_alloc_sem);
  1351. if (written > 0 && sync) {
  1352. ssize_t err;
  1353. err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
  1354. if (err < 0)
  1355. written = err;
  1356. }
  1357. mutex_unlock(&inode->i_mutex);
  1358. mlog_exit(ret);
  1359. return written ? written : ret;
  1360. }
  1361. static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
  1362. struct pipe_buffer *buf,
  1363. struct splice_desc *sd)
  1364. {
  1365. int ret, count, total = 0;
  1366. ssize_t copied = 0;
  1367. struct ocfs2_splice_write_priv sp;
  1368. ret = buf->ops->pin(pipe, buf);
  1369. if (ret)
  1370. goto out;
  1371. sp.s_sd = sd;
  1372. sp.s_buf = buf;
  1373. sp.s_pipe = pipe;
  1374. sp.s_offset = sd->pos & ~PAGE_CACHE_MASK;
  1375. sp.s_buf_offset = buf->offset;
  1376. count = sd->len;
  1377. if (count + sp.s_offset > PAGE_CACHE_SIZE)
  1378. count = PAGE_CACHE_SIZE - sp.s_offset;
  1379. do {
  1380. /*
  1381. * splice wants us to copy up to one page at a
  1382. * time. For pagesize > cluster size, this means we
  1383. * might enter ocfs2_buffered_write_cluster() more
  1384. * than once, so keep track of our progress here.
  1385. */
  1386. copied = ocfs2_buffered_write_cluster(sd->file,
  1387. (loff_t)sd->pos + total,
  1388. count,
  1389. ocfs2_map_and_write_splice_data,
  1390. &sp);
  1391. if (copied < 0) {
  1392. mlog_errno(copied);
  1393. ret = copied;
  1394. goto out;
  1395. }
  1396. count -= copied;
  1397. sp.s_offset += copied;
  1398. sp.s_buf_offset += copied;
  1399. total += copied;
  1400. } while (count);
  1401. ret = 0;
  1402. out:
  1403. return total ? total : ret;
  1404. }
  1405. static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1406. struct file *out,
  1407. loff_t *ppos,
  1408. size_t len,
  1409. unsigned int flags)
  1410. {
  1411. int ret, err;
  1412. struct address_space *mapping = out->f_mapping;
  1413. struct inode *inode = mapping->host;
  1414. ret = __splice_from_pipe(pipe, out, ppos, len, flags,
  1415. ocfs2_splice_write_actor);
  1416. if (ret > 0) {
  1417. *ppos += ret;
  1418. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1419. err = generic_osync_inode(inode, mapping,
  1420. OSYNC_METADATA|OSYNC_DATA);
  1421. if (err)
  1422. ret = err;
  1423. }
  1424. }
  1425. return ret;
  1426. }
  1427. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1428. struct file *out,
  1429. loff_t *ppos,
  1430. size_t len,
  1431. unsigned int flags)
  1432. {
  1433. int ret;
  1434. struct inode *inode = out->f_path.dentry->d_inode;
  1435. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1436. (unsigned int)len,
  1437. out->f_path.dentry->d_name.len,
  1438. out->f_path.dentry->d_name.name);
  1439. inode_double_lock(inode, pipe->inode);
  1440. ret = ocfs2_rw_lock(inode, 1);
  1441. if (ret < 0) {
  1442. mlog_errno(ret);
  1443. goto out;
  1444. }
  1445. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1446. NULL);
  1447. if (ret < 0) {
  1448. mlog_errno(ret);
  1449. goto out_unlock;
  1450. }
  1451. /* ok, we're done with i_size and alloc work */
  1452. ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
  1453. out_unlock:
  1454. ocfs2_rw_unlock(inode, 1);
  1455. out:
  1456. inode_double_unlock(inode, pipe->inode);
  1457. mlog_exit(ret);
  1458. return ret;
  1459. }
  1460. static ssize_t ocfs2_file_splice_read(struct file *in,
  1461. loff_t *ppos,
  1462. struct pipe_inode_info *pipe,
  1463. size_t len,
  1464. unsigned int flags)
  1465. {
  1466. int ret = 0;
  1467. struct inode *inode = in->f_path.dentry->d_inode;
  1468. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1469. (unsigned int)len,
  1470. in->f_path.dentry->d_name.len,
  1471. in->f_path.dentry->d_name.name);
  1472. /*
  1473. * See the comment in ocfs2_file_aio_read()
  1474. */
  1475. ret = ocfs2_meta_lock(inode, NULL, 0);
  1476. if (ret < 0) {
  1477. mlog_errno(ret);
  1478. goto bail;
  1479. }
  1480. ocfs2_meta_unlock(inode, 0);
  1481. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1482. bail:
  1483. mlog_exit(ret);
  1484. return ret;
  1485. }
  1486. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1487. const struct iovec *iov,
  1488. unsigned long nr_segs,
  1489. loff_t pos)
  1490. {
  1491. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1492. struct file *filp = iocb->ki_filp;
  1493. struct inode *inode = filp->f_path.dentry->d_inode;
  1494. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1495. (unsigned int)nr_segs,
  1496. filp->f_path.dentry->d_name.len,
  1497. filp->f_path.dentry->d_name.name);
  1498. if (!inode) {
  1499. ret = -EINVAL;
  1500. mlog_errno(ret);
  1501. goto bail;
  1502. }
  1503. /*
  1504. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1505. * need locks to protect pending reads from racing with truncate.
  1506. */
  1507. if (filp->f_flags & O_DIRECT) {
  1508. down_read(&inode->i_alloc_sem);
  1509. have_alloc_sem = 1;
  1510. ret = ocfs2_rw_lock(inode, 0);
  1511. if (ret < 0) {
  1512. mlog_errno(ret);
  1513. goto bail;
  1514. }
  1515. rw_level = 0;
  1516. /* communicate with ocfs2_dio_end_io */
  1517. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1518. }
  1519. /*
  1520. * We're fine letting folks race truncates and extending
  1521. * writes with read across the cluster, just like they can
  1522. * locally. Hence no rw_lock during read.
  1523. *
  1524. * Take and drop the meta data lock to update inode fields
  1525. * like i_size. This allows the checks down below
  1526. * generic_file_aio_read() a chance of actually working.
  1527. */
  1528. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1529. if (ret < 0) {
  1530. mlog_errno(ret);
  1531. goto bail;
  1532. }
  1533. ocfs2_meta_unlock(inode, lock_level);
  1534. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1535. if (ret == -EINVAL)
  1536. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1537. /* buffered aio wouldn't have proper lock coverage today */
  1538. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1539. /* see ocfs2_file_aio_write */
  1540. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1541. rw_level = -1;
  1542. have_alloc_sem = 0;
  1543. }
  1544. bail:
  1545. if (have_alloc_sem)
  1546. up_read(&inode->i_alloc_sem);
  1547. if (rw_level != -1)
  1548. ocfs2_rw_unlock(inode, rw_level);
  1549. mlog_exit(ret);
  1550. return ret;
  1551. }
  1552. const struct inode_operations ocfs2_file_iops = {
  1553. .setattr = ocfs2_setattr,
  1554. .getattr = ocfs2_getattr,
  1555. .permission = ocfs2_permission,
  1556. };
  1557. const struct inode_operations ocfs2_special_file_iops = {
  1558. .setattr = ocfs2_setattr,
  1559. .getattr = ocfs2_getattr,
  1560. .permission = ocfs2_permission,
  1561. };
  1562. const struct file_operations ocfs2_fops = {
  1563. .read = do_sync_read,
  1564. .write = do_sync_write,
  1565. .sendfile = generic_file_sendfile,
  1566. .mmap = ocfs2_mmap,
  1567. .fsync = ocfs2_sync_file,
  1568. .release = ocfs2_file_release,
  1569. .open = ocfs2_file_open,
  1570. .aio_read = ocfs2_file_aio_read,
  1571. .aio_write = ocfs2_file_aio_write,
  1572. .ioctl = ocfs2_ioctl,
  1573. #ifdef CONFIG_COMPAT
  1574. .compat_ioctl = ocfs2_compat_ioctl,
  1575. #endif
  1576. .splice_read = ocfs2_file_splice_read,
  1577. .splice_write = ocfs2_file_splice_write,
  1578. };
  1579. const struct file_operations ocfs2_dops = {
  1580. .read = generic_read_dir,
  1581. .readdir = ocfs2_readdir,
  1582. .fsync = ocfs2_sync_file,
  1583. .ioctl = ocfs2_ioctl,
  1584. #ifdef CONFIG_COMPAT
  1585. .compat_ioctl = ocfs2_compat_ioctl,
  1586. #endif
  1587. };