file.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. #define MLOG_MASK_PREFIX ML_INODE
  36. #include <cluster/masklog.h>
  37. #include "ocfs2.h"
  38. #include "alloc.h"
  39. #include "aops.h"
  40. #include "dir.h"
  41. #include "dlmglue.h"
  42. #include "extent_map.h"
  43. #include "file.h"
  44. #include "sysfile.h"
  45. #include "inode.h"
  46. #include "ioctl.h"
  47. #include "journal.h"
  48. #include "mmap.h"
  49. #include "suballoc.h"
  50. #include "super.h"
  51. #include "buffer_head_io.h"
  52. static int ocfs2_sync_inode(struct inode *inode)
  53. {
  54. filemap_fdatawrite(inode->i_mapping);
  55. return sync_mapping_buffers(inode->i_mapping);
  56. }
  57. static int ocfs2_file_open(struct inode *inode, struct file *file)
  58. {
  59. int status;
  60. int mode = file->f_flags;
  61. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  62. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  63. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  64. spin_lock(&oi->ip_lock);
  65. /* Check that the inode hasn't been wiped from disk by another
  66. * node. If it hasn't then we're safe as long as we hold the
  67. * spin lock until our increment of open count. */
  68. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  69. spin_unlock(&oi->ip_lock);
  70. status = -ENOENT;
  71. goto leave;
  72. }
  73. if (mode & O_DIRECT)
  74. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  75. oi->ip_open_count++;
  76. spin_unlock(&oi->ip_lock);
  77. status = 0;
  78. leave:
  79. mlog_exit(status);
  80. return status;
  81. }
  82. static int ocfs2_file_release(struct inode *inode, struct file *file)
  83. {
  84. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  85. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  86. file->f_path.dentry->d_name.len,
  87. file->f_path.dentry->d_name.name);
  88. spin_lock(&oi->ip_lock);
  89. if (!--oi->ip_open_count)
  90. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  91. spin_unlock(&oi->ip_lock);
  92. mlog_exit(0);
  93. return 0;
  94. }
  95. static int ocfs2_sync_file(struct file *file,
  96. struct dentry *dentry,
  97. int datasync)
  98. {
  99. int err = 0;
  100. journal_t *journal;
  101. struct inode *inode = dentry->d_inode;
  102. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  103. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  104. dentry->d_name.len, dentry->d_name.name);
  105. err = ocfs2_sync_inode(dentry->d_inode);
  106. if (err)
  107. goto bail;
  108. journal = osb->journal->j_journal;
  109. err = journal_force_commit(journal);
  110. bail:
  111. mlog_exit(err);
  112. return (err < 0) ? -EIO : 0;
  113. }
  114. int ocfs2_should_update_atime(struct inode *inode,
  115. struct vfsmount *vfsmnt)
  116. {
  117. struct timespec now;
  118. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  119. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  120. return 0;
  121. if ((inode->i_flags & S_NOATIME) ||
  122. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  123. return 0;
  124. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  125. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  126. return 0;
  127. now = CURRENT_TIME;
  128. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  129. return 0;
  130. else
  131. return 1;
  132. }
  133. int ocfs2_update_inode_atime(struct inode *inode,
  134. struct buffer_head *bh)
  135. {
  136. int ret;
  137. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  138. handle_t *handle;
  139. mlog_entry_void();
  140. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  141. if (handle == NULL) {
  142. ret = -ENOMEM;
  143. mlog_errno(ret);
  144. goto out;
  145. }
  146. inode->i_atime = CURRENT_TIME;
  147. ret = ocfs2_mark_inode_dirty(handle, inode, bh);
  148. if (ret < 0)
  149. mlog_errno(ret);
  150. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  151. out:
  152. mlog_exit(ret);
  153. return ret;
  154. }
  155. int ocfs2_set_inode_size(handle_t *handle,
  156. struct inode *inode,
  157. struct buffer_head *fe_bh,
  158. u64 new_i_size)
  159. {
  160. int status;
  161. mlog_entry_void();
  162. i_size_write(inode, new_i_size);
  163. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  164. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  165. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  166. if (status < 0) {
  167. mlog_errno(status);
  168. goto bail;
  169. }
  170. bail:
  171. mlog_exit(status);
  172. return status;
  173. }
  174. static int ocfs2_simple_size_update(struct inode *inode,
  175. struct buffer_head *di_bh,
  176. u64 new_i_size)
  177. {
  178. int ret;
  179. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  180. handle_t *handle = NULL;
  181. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  182. if (handle == NULL) {
  183. ret = -ENOMEM;
  184. mlog_errno(ret);
  185. goto out;
  186. }
  187. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  188. new_i_size);
  189. if (ret < 0)
  190. mlog_errno(ret);
  191. ocfs2_commit_trans(osb, handle);
  192. out:
  193. return ret;
  194. }
  195. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  196. struct inode *inode,
  197. struct buffer_head *fe_bh,
  198. u64 new_i_size)
  199. {
  200. int status;
  201. handle_t *handle;
  202. mlog_entry_void();
  203. /* TODO: This needs to actually orphan the inode in this
  204. * transaction. */
  205. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  206. if (IS_ERR(handle)) {
  207. status = PTR_ERR(handle);
  208. mlog_errno(status);
  209. goto out;
  210. }
  211. status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
  212. if (status < 0)
  213. mlog_errno(status);
  214. ocfs2_commit_trans(osb, handle);
  215. out:
  216. mlog_exit(status);
  217. return status;
  218. }
  219. static int ocfs2_truncate_file(struct inode *inode,
  220. struct buffer_head *di_bh,
  221. u64 new_i_size)
  222. {
  223. int status = 0;
  224. struct ocfs2_dinode *fe = NULL;
  225. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  226. struct ocfs2_truncate_context *tc = NULL;
  227. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  228. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  229. (unsigned long long)new_i_size);
  230. truncate_inode_pages(inode->i_mapping, new_i_size);
  231. fe = (struct ocfs2_dinode *) di_bh->b_data;
  232. if (!OCFS2_IS_VALID_DINODE(fe)) {
  233. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  234. status = -EIO;
  235. goto bail;
  236. }
  237. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  238. "Inode %llu, inode i_size = %lld != di "
  239. "i_size = %llu, i_flags = 0x%x\n",
  240. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  241. i_size_read(inode),
  242. (unsigned long long)le64_to_cpu(fe->i_size),
  243. le32_to_cpu(fe->i_flags));
  244. if (new_i_size > le64_to_cpu(fe->i_size)) {
  245. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  246. (unsigned long long)le64_to_cpu(fe->i_size),
  247. (unsigned long long)new_i_size);
  248. status = -EINVAL;
  249. mlog_errno(status);
  250. goto bail;
  251. }
  252. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  253. (unsigned long long)le64_to_cpu(fe->i_blkno),
  254. (unsigned long long)le64_to_cpu(fe->i_size),
  255. (unsigned long long)new_i_size);
  256. /* lets handle the simple truncate cases before doing any more
  257. * cluster locking. */
  258. if (new_i_size == le64_to_cpu(fe->i_size))
  259. goto bail;
  260. /* This forces other nodes to sync and drop their pages. Do
  261. * this even if we have a truncate without allocation change -
  262. * ocfs2 cluster sizes can be much greater than page size, so
  263. * we have to truncate them anyway. */
  264. status = ocfs2_data_lock(inode, 1);
  265. if (status < 0) {
  266. mlog_errno(status);
  267. goto bail;
  268. }
  269. ocfs2_data_unlock(inode, 1);
  270. if (le32_to_cpu(fe->i_clusters) ==
  271. ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
  272. mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
  273. fe->i_clusters);
  274. /* No allocation change is required, so lets fast path
  275. * this truncate. */
  276. status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  277. if (status < 0)
  278. mlog_errno(status);
  279. goto bail;
  280. }
  281. /* alright, we're going to need to do a full blown alloc size
  282. * change. Orphan the inode so that recovery can complete the
  283. * truncate if necessary. This does the task of marking
  284. * i_size. */
  285. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  286. if (status < 0) {
  287. mlog_errno(status);
  288. goto bail;
  289. }
  290. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  291. if (status < 0) {
  292. mlog_errno(status);
  293. goto bail;
  294. }
  295. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  296. if (status < 0) {
  297. mlog_errno(status);
  298. goto bail;
  299. }
  300. /* TODO: orphan dir cleanup here. */
  301. bail:
  302. mlog_exit(status);
  303. return status;
  304. }
  305. /*
  306. * extend allocation only here.
  307. * we'll update all the disk stuff, and oip->alloc_size
  308. *
  309. * expect stuff to be locked, a transaction started and enough data /
  310. * metadata reservations in the contexts.
  311. *
  312. * Will return -EAGAIN, and a reason if a restart is needed.
  313. * If passed in, *reason will always be set, even in error.
  314. */
  315. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  316. struct inode *inode,
  317. u32 clusters_to_add,
  318. struct buffer_head *fe_bh,
  319. handle_t *handle,
  320. struct ocfs2_alloc_context *data_ac,
  321. struct ocfs2_alloc_context *meta_ac,
  322. enum ocfs2_alloc_restarted *reason_ret)
  323. {
  324. int status = 0;
  325. int free_extents;
  326. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  327. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  328. u32 bit_off, num_bits;
  329. u64 block;
  330. BUG_ON(!clusters_to_add);
  331. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  332. if (free_extents < 0) {
  333. status = free_extents;
  334. mlog_errno(status);
  335. goto leave;
  336. }
  337. /* there are two cases which could cause us to EAGAIN in the
  338. * we-need-more-metadata case:
  339. * 1) we haven't reserved *any*
  340. * 2) we are so fragmented, we've needed to add metadata too
  341. * many times. */
  342. if (!free_extents && !meta_ac) {
  343. mlog(0, "we haven't reserved any metadata!\n");
  344. status = -EAGAIN;
  345. reason = RESTART_META;
  346. goto leave;
  347. } else if ((!free_extents)
  348. && (ocfs2_alloc_context_bits_left(meta_ac)
  349. < ocfs2_extend_meta_needed(fe))) {
  350. mlog(0, "filesystem is really fragmented...\n");
  351. status = -EAGAIN;
  352. reason = RESTART_META;
  353. goto leave;
  354. }
  355. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  356. &bit_off, &num_bits);
  357. if (status < 0) {
  358. if (status != -ENOSPC)
  359. mlog_errno(status);
  360. goto leave;
  361. }
  362. BUG_ON(num_bits > clusters_to_add);
  363. /* reserve our write early -- insert_extent may update the inode */
  364. status = ocfs2_journal_access(handle, inode, fe_bh,
  365. OCFS2_JOURNAL_ACCESS_WRITE);
  366. if (status < 0) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  371. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  372. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  373. status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
  374. num_bits, meta_ac);
  375. if (status < 0) {
  376. mlog_errno(status);
  377. goto leave;
  378. }
  379. le32_add_cpu(&fe->i_clusters, num_bits);
  380. spin_lock(&OCFS2_I(inode)->ip_lock);
  381. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  382. spin_unlock(&OCFS2_I(inode)->ip_lock);
  383. status = ocfs2_journal_dirty(handle, fe_bh);
  384. if (status < 0) {
  385. mlog_errno(status);
  386. goto leave;
  387. }
  388. clusters_to_add -= num_bits;
  389. if (clusters_to_add) {
  390. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  391. "%u\n", fe->i_clusters, clusters_to_add);
  392. status = -EAGAIN;
  393. reason = RESTART_TRANS;
  394. }
  395. leave:
  396. mlog_exit(status);
  397. if (reason_ret)
  398. *reason_ret = reason;
  399. return status;
  400. }
  401. static int ocfs2_extend_allocation(struct inode *inode,
  402. u32 clusters_to_add)
  403. {
  404. int status = 0;
  405. int restart_func = 0;
  406. int drop_alloc_sem = 0;
  407. int credits, num_free_extents;
  408. u32 prev_clusters;
  409. struct buffer_head *bh = NULL;
  410. struct ocfs2_dinode *fe = NULL;
  411. handle_t *handle = NULL;
  412. struct ocfs2_alloc_context *data_ac = NULL;
  413. struct ocfs2_alloc_context *meta_ac = NULL;
  414. enum ocfs2_alloc_restarted why;
  415. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  416. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  417. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  418. OCFS2_BH_CACHED, inode);
  419. if (status < 0) {
  420. mlog_errno(status);
  421. goto leave;
  422. }
  423. fe = (struct ocfs2_dinode *) bh->b_data;
  424. if (!OCFS2_IS_VALID_DINODE(fe)) {
  425. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  426. status = -EIO;
  427. goto leave;
  428. }
  429. restart_all:
  430. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  431. mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
  432. "clusters_to_add = %u\n",
  433. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  434. fe->i_clusters, clusters_to_add);
  435. num_free_extents = ocfs2_num_free_extents(osb,
  436. inode,
  437. fe);
  438. if (num_free_extents < 0) {
  439. status = num_free_extents;
  440. mlog_errno(status);
  441. goto leave;
  442. }
  443. if (!num_free_extents) {
  444. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  445. if (status < 0) {
  446. if (status != -ENOSPC)
  447. mlog_errno(status);
  448. goto leave;
  449. }
  450. }
  451. status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac);
  452. if (status < 0) {
  453. if (status != -ENOSPC)
  454. mlog_errno(status);
  455. goto leave;
  456. }
  457. /* blocks peope in read/write from reading our allocation
  458. * until we're done changing it. We depend on i_mutex to block
  459. * other extend/truncate calls while we're here. Ordering wrt
  460. * start_trans is important here -- always do it before! */
  461. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  462. drop_alloc_sem = 1;
  463. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  464. handle = ocfs2_start_trans(osb, credits);
  465. if (IS_ERR(handle)) {
  466. status = PTR_ERR(handle);
  467. handle = NULL;
  468. mlog_errno(status);
  469. goto leave;
  470. }
  471. restarted_transaction:
  472. /* reserve a write to the file entry early on - that we if we
  473. * run out of credits in the allocation path, we can still
  474. * update i_size. */
  475. status = ocfs2_journal_access(handle, inode, bh,
  476. OCFS2_JOURNAL_ACCESS_WRITE);
  477. if (status < 0) {
  478. mlog_errno(status);
  479. goto leave;
  480. }
  481. prev_clusters = OCFS2_I(inode)->ip_clusters;
  482. status = ocfs2_do_extend_allocation(osb,
  483. inode,
  484. clusters_to_add,
  485. bh,
  486. handle,
  487. data_ac,
  488. meta_ac,
  489. &why);
  490. if ((status < 0) && (status != -EAGAIN)) {
  491. if (status != -ENOSPC)
  492. mlog_errno(status);
  493. goto leave;
  494. }
  495. status = ocfs2_journal_dirty(handle, bh);
  496. if (status < 0) {
  497. mlog_errno(status);
  498. goto leave;
  499. }
  500. spin_lock(&OCFS2_I(inode)->ip_lock);
  501. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  502. spin_unlock(&OCFS2_I(inode)->ip_lock);
  503. if (why != RESTART_NONE && clusters_to_add) {
  504. if (why == RESTART_META) {
  505. mlog(0, "restarting function.\n");
  506. restart_func = 1;
  507. } else {
  508. BUG_ON(why != RESTART_TRANS);
  509. mlog(0, "restarting transaction.\n");
  510. /* TODO: This can be more intelligent. */
  511. credits = ocfs2_calc_extend_credits(osb->sb,
  512. fe,
  513. clusters_to_add);
  514. status = ocfs2_extend_trans(handle, credits);
  515. if (status < 0) {
  516. /* handle still has to be committed at
  517. * this point. */
  518. status = -ENOMEM;
  519. mlog_errno(status);
  520. goto leave;
  521. }
  522. goto restarted_transaction;
  523. }
  524. }
  525. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  526. fe->i_clusters, (unsigned long long)fe->i_size);
  527. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  528. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  529. leave:
  530. if (drop_alloc_sem) {
  531. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  532. drop_alloc_sem = 0;
  533. }
  534. if (handle) {
  535. ocfs2_commit_trans(osb, handle);
  536. handle = NULL;
  537. }
  538. if (data_ac) {
  539. ocfs2_free_alloc_context(data_ac);
  540. data_ac = NULL;
  541. }
  542. if (meta_ac) {
  543. ocfs2_free_alloc_context(meta_ac);
  544. meta_ac = NULL;
  545. }
  546. if ((!status) && restart_func) {
  547. restart_func = 0;
  548. goto restart_all;
  549. }
  550. if (bh) {
  551. brelse(bh);
  552. bh = NULL;
  553. }
  554. mlog_exit(status);
  555. return status;
  556. }
  557. /* Some parts of this taken from generic_cont_expand, which turned out
  558. * to be too fragile to do exactly what we need without us having to
  559. * worry about recursive locking in ->prepare_write() and
  560. * ->commit_write(). */
  561. static int ocfs2_write_zero_page(struct inode *inode,
  562. u64 size)
  563. {
  564. struct address_space *mapping = inode->i_mapping;
  565. struct page *page;
  566. unsigned long index;
  567. unsigned int offset;
  568. handle_t *handle = NULL;
  569. int ret;
  570. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  571. /* ugh. in prepare/commit_write, if from==to==start of block, we
  572. ** skip the prepare. make sure we never send an offset for the start
  573. ** of a block
  574. */
  575. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  576. offset++;
  577. }
  578. index = size >> PAGE_CACHE_SHIFT;
  579. page = grab_cache_page(mapping, index);
  580. if (!page) {
  581. ret = -ENOMEM;
  582. mlog_errno(ret);
  583. goto out;
  584. }
  585. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  586. if (ret < 0) {
  587. mlog_errno(ret);
  588. goto out_unlock;
  589. }
  590. if (ocfs2_should_order_data(inode)) {
  591. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  592. offset);
  593. if (IS_ERR(handle)) {
  594. ret = PTR_ERR(handle);
  595. handle = NULL;
  596. goto out_unlock;
  597. }
  598. }
  599. /* must not update i_size! */
  600. ret = block_commit_write(page, offset, offset);
  601. if (ret < 0)
  602. mlog_errno(ret);
  603. else
  604. ret = 0;
  605. if (handle)
  606. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  607. out_unlock:
  608. unlock_page(page);
  609. page_cache_release(page);
  610. out:
  611. return ret;
  612. }
  613. static int ocfs2_zero_extend(struct inode *inode,
  614. u64 zero_to_size)
  615. {
  616. int ret = 0;
  617. u64 start_off;
  618. struct super_block *sb = inode->i_sb;
  619. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  620. while (start_off < zero_to_size) {
  621. ret = ocfs2_write_zero_page(inode, start_off);
  622. if (ret < 0) {
  623. mlog_errno(ret);
  624. goto out;
  625. }
  626. start_off += sb->s_blocksize;
  627. /*
  628. * Very large extends have the potential to lock up
  629. * the cpu for extended periods of time.
  630. */
  631. cond_resched();
  632. }
  633. out:
  634. return ret;
  635. }
  636. /*
  637. * A tail_to_skip value > 0 indicates that we're being called from
  638. * ocfs2_file_aio_write(). This has the following implications:
  639. *
  640. * - we don't want to update i_size
  641. * - di_bh will be NULL, which is fine because it's only used in the
  642. * case where we want to update i_size.
  643. * - ocfs2_zero_extend() will then only be filling the hole created
  644. * between i_size and the start of the write.
  645. */
  646. static int ocfs2_extend_file(struct inode *inode,
  647. struct buffer_head *di_bh,
  648. u64 new_i_size,
  649. size_t tail_to_skip)
  650. {
  651. int ret = 0;
  652. u32 clusters_to_add;
  653. BUG_ON(!tail_to_skip && !di_bh);
  654. /* setattr sometimes calls us like this. */
  655. if (new_i_size == 0)
  656. goto out;
  657. if (i_size_read(inode) == new_i_size)
  658. goto out;
  659. BUG_ON(new_i_size < i_size_read(inode));
  660. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  661. OCFS2_I(inode)->ip_clusters;
  662. /*
  663. * protect the pages that ocfs2_zero_extend is going to be
  664. * pulling into the page cache.. we do this before the
  665. * metadata extend so that we don't get into the situation
  666. * where we've extended the metadata but can't get the data
  667. * lock to zero.
  668. */
  669. ret = ocfs2_data_lock(inode, 1);
  670. if (ret < 0) {
  671. mlog_errno(ret);
  672. goto out;
  673. }
  674. if (clusters_to_add) {
  675. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  676. if (ret < 0) {
  677. mlog_errno(ret);
  678. goto out_unlock;
  679. }
  680. }
  681. /*
  682. * Call this even if we don't add any clusters to the tree. We
  683. * still need to zero the area between the old i_size and the
  684. * new i_size.
  685. */
  686. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  687. if (ret < 0) {
  688. mlog_errno(ret);
  689. goto out_unlock;
  690. }
  691. if (!tail_to_skip) {
  692. /* We're being called from ocfs2_setattr() which wants
  693. * us to update i_size */
  694. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  695. if (ret < 0)
  696. mlog_errno(ret);
  697. }
  698. out_unlock:
  699. ocfs2_data_unlock(inode, 1);
  700. out:
  701. return ret;
  702. }
  703. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  704. {
  705. int status = 0, size_change;
  706. struct inode *inode = dentry->d_inode;
  707. struct super_block *sb = inode->i_sb;
  708. struct ocfs2_super *osb = OCFS2_SB(sb);
  709. struct buffer_head *bh = NULL;
  710. handle_t *handle = NULL;
  711. mlog_entry("(0x%p, '%.*s')\n", dentry,
  712. dentry->d_name.len, dentry->d_name.name);
  713. if (attr->ia_valid & ATTR_MODE)
  714. mlog(0, "mode change: %d\n", attr->ia_mode);
  715. if (attr->ia_valid & ATTR_UID)
  716. mlog(0, "uid change: %d\n", attr->ia_uid);
  717. if (attr->ia_valid & ATTR_GID)
  718. mlog(0, "gid change: %d\n", attr->ia_gid);
  719. if (attr->ia_valid & ATTR_SIZE)
  720. mlog(0, "size change...\n");
  721. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  722. mlog(0, "time change...\n");
  723. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  724. | ATTR_GID | ATTR_UID | ATTR_MODE)
  725. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  726. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  727. return 0;
  728. }
  729. status = inode_change_ok(inode, attr);
  730. if (status)
  731. return status;
  732. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  733. if (size_change) {
  734. status = ocfs2_rw_lock(inode, 1);
  735. if (status < 0) {
  736. mlog_errno(status);
  737. goto bail;
  738. }
  739. }
  740. status = ocfs2_meta_lock(inode, &bh, 1);
  741. if (status < 0) {
  742. if (status != -ENOENT)
  743. mlog_errno(status);
  744. goto bail_unlock_rw;
  745. }
  746. if (size_change && attr->ia_size != i_size_read(inode)) {
  747. if (i_size_read(inode) > attr->ia_size)
  748. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  749. else
  750. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  751. if (status < 0) {
  752. if (status != -ENOSPC)
  753. mlog_errno(status);
  754. status = -ENOSPC;
  755. goto bail_unlock;
  756. }
  757. }
  758. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  759. if (IS_ERR(handle)) {
  760. status = PTR_ERR(handle);
  761. mlog_errno(status);
  762. goto bail_unlock;
  763. }
  764. status = inode_setattr(inode, attr);
  765. if (status < 0) {
  766. mlog_errno(status);
  767. goto bail_commit;
  768. }
  769. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  770. if (status < 0)
  771. mlog_errno(status);
  772. bail_commit:
  773. ocfs2_commit_trans(osb, handle);
  774. bail_unlock:
  775. ocfs2_meta_unlock(inode, 1);
  776. bail_unlock_rw:
  777. if (size_change)
  778. ocfs2_rw_unlock(inode, 1);
  779. bail:
  780. if (bh)
  781. brelse(bh);
  782. mlog_exit(status);
  783. return status;
  784. }
  785. int ocfs2_getattr(struct vfsmount *mnt,
  786. struct dentry *dentry,
  787. struct kstat *stat)
  788. {
  789. struct inode *inode = dentry->d_inode;
  790. struct super_block *sb = dentry->d_inode->i_sb;
  791. struct ocfs2_super *osb = sb->s_fs_info;
  792. int err;
  793. mlog_entry_void();
  794. err = ocfs2_inode_revalidate(dentry);
  795. if (err) {
  796. if (err != -ENOENT)
  797. mlog_errno(err);
  798. goto bail;
  799. }
  800. generic_fillattr(inode, stat);
  801. /* We set the blksize from the cluster size for performance */
  802. stat->blksize = osb->s_clustersize;
  803. bail:
  804. mlog_exit(err);
  805. return err;
  806. }
  807. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  808. {
  809. int ret;
  810. mlog_entry_void();
  811. ret = ocfs2_meta_lock(inode, NULL, 0);
  812. if (ret) {
  813. mlog_errno(ret);
  814. goto out;
  815. }
  816. ret = generic_permission(inode, mask, NULL);
  817. if (ret)
  818. mlog_errno(ret);
  819. ocfs2_meta_unlock(inode, 0);
  820. out:
  821. mlog_exit(ret);
  822. return ret;
  823. }
  824. static int ocfs2_write_remove_suid(struct inode *inode)
  825. {
  826. int ret;
  827. struct buffer_head *bh = NULL;
  828. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  829. handle_t *handle;
  830. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  831. struct ocfs2_dinode *di;
  832. mlog_entry("(Inode %llu, mode 0%o)\n",
  833. (unsigned long long)oi->ip_blkno, inode->i_mode);
  834. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  835. if (handle == NULL) {
  836. ret = -ENOMEM;
  837. mlog_errno(ret);
  838. goto out;
  839. }
  840. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  841. if (ret < 0) {
  842. mlog_errno(ret);
  843. goto out_trans;
  844. }
  845. ret = ocfs2_journal_access(handle, inode, bh,
  846. OCFS2_JOURNAL_ACCESS_WRITE);
  847. if (ret < 0) {
  848. mlog_errno(ret);
  849. goto out_bh;
  850. }
  851. inode->i_mode &= ~S_ISUID;
  852. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  853. inode->i_mode &= ~S_ISGID;
  854. di = (struct ocfs2_dinode *) bh->b_data;
  855. di->i_mode = cpu_to_le16(inode->i_mode);
  856. ret = ocfs2_journal_dirty(handle, bh);
  857. if (ret < 0)
  858. mlog_errno(ret);
  859. out_bh:
  860. brelse(bh);
  861. out_trans:
  862. ocfs2_commit_trans(osb, handle);
  863. out:
  864. mlog_exit(ret);
  865. return ret;
  866. }
  867. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  868. loff_t *ppos,
  869. size_t count,
  870. int appending)
  871. {
  872. int ret = 0, meta_level = appending;
  873. struct inode *inode = dentry->d_inode;
  874. u32 clusters;
  875. loff_t newsize, saved_pos;
  876. /*
  877. * We sample i_size under a read level meta lock to see if our write
  878. * is extending the file, if it is we back off and get a write level
  879. * meta lock.
  880. */
  881. for(;;) {
  882. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  883. if (ret < 0) {
  884. meta_level = -1;
  885. mlog_errno(ret);
  886. goto out;
  887. }
  888. /* Clear suid / sgid if necessary. We do this here
  889. * instead of later in the write path because
  890. * remove_suid() calls ->setattr without any hint that
  891. * we may have already done our cluster locking. Since
  892. * ocfs2_setattr() *must* take cluster locks to
  893. * proceeed, this will lead us to recursively lock the
  894. * inode. There's also the dinode i_size state which
  895. * can be lost via setattr during extending writes (we
  896. * set inode->i_size at the end of a write. */
  897. if (should_remove_suid(dentry)) {
  898. if (meta_level == 0) {
  899. ocfs2_meta_unlock(inode, meta_level);
  900. meta_level = 1;
  901. continue;
  902. }
  903. ret = ocfs2_write_remove_suid(inode);
  904. if (ret < 0) {
  905. mlog_errno(ret);
  906. goto out_unlock;
  907. }
  908. }
  909. /* work on a copy of ppos until we're sure that we won't have
  910. * to recalculate it due to relocking. */
  911. if (appending) {
  912. saved_pos = i_size_read(inode);
  913. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  914. } else {
  915. saved_pos = *ppos;
  916. }
  917. newsize = count + saved_pos;
  918. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  919. (long long) saved_pos, (long long) newsize,
  920. (long long) i_size_read(inode));
  921. /* No need for a higher level metadata lock if we're
  922. * never going past i_size. */
  923. if (newsize <= i_size_read(inode))
  924. break;
  925. if (meta_level == 0) {
  926. ocfs2_meta_unlock(inode, meta_level);
  927. meta_level = 1;
  928. continue;
  929. }
  930. spin_lock(&OCFS2_I(inode)->ip_lock);
  931. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  932. OCFS2_I(inode)->ip_clusters;
  933. spin_unlock(&OCFS2_I(inode)->ip_lock);
  934. mlog(0, "Writing at EOF, may need more allocation: "
  935. "i_size = %lld, newsize = %lld, need %u clusters\n",
  936. (long long) i_size_read(inode), (long long) newsize,
  937. clusters);
  938. /* We only want to continue the rest of this loop if
  939. * our extend will actually require more
  940. * allocation. */
  941. if (!clusters)
  942. break;
  943. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  944. if (ret < 0) {
  945. if (ret != -ENOSPC)
  946. mlog_errno(ret);
  947. goto out_unlock;
  948. }
  949. break;
  950. }
  951. if (appending)
  952. *ppos = saved_pos;
  953. out_unlock:
  954. ocfs2_meta_unlock(inode, meta_level);
  955. out:
  956. return ret;
  957. }
  958. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  959. const struct iovec *iov,
  960. unsigned long nr_segs,
  961. loff_t pos)
  962. {
  963. int ret, rw_level, have_alloc_sem = 0;
  964. struct file *filp = iocb->ki_filp;
  965. struct inode *inode = filp->f_path.dentry->d_inode;
  966. int appending = filp->f_flags & O_APPEND ? 1 : 0;
  967. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  968. (unsigned int)nr_segs,
  969. filp->f_path.dentry->d_name.len,
  970. filp->f_path.dentry->d_name.name);
  971. /* happy write of zero bytes */
  972. if (iocb->ki_left == 0)
  973. return 0;
  974. mutex_lock(&inode->i_mutex);
  975. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  976. if (filp->f_flags & O_DIRECT) {
  977. have_alloc_sem = 1;
  978. down_read(&inode->i_alloc_sem);
  979. }
  980. /* concurrent O_DIRECT writes are allowed */
  981. rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
  982. ret = ocfs2_rw_lock(inode, rw_level);
  983. if (ret < 0) {
  984. rw_level = -1;
  985. mlog_errno(ret);
  986. goto out;
  987. }
  988. ret = ocfs2_prepare_inode_for_write(filp->f_path.dentry, &iocb->ki_pos,
  989. iocb->ki_left, appending);
  990. if (ret < 0) {
  991. mlog_errno(ret);
  992. goto out;
  993. }
  994. /* communicate with ocfs2_dio_end_io */
  995. ocfs2_iocb_set_rw_locked(iocb);
  996. ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
  997. /* buffered aio wouldn't have proper lock coverage today */
  998. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  999. /*
  1000. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1001. * function pointer which is called when o_direct io completes so that
  1002. * it can unlock our rw lock. (it's the clustered equivalent of
  1003. * i_alloc_sem; protects truncate from racing with pending ios).
  1004. * Unfortunately there are error cases which call end_io and others
  1005. * that don't. so we don't have to unlock the rw_lock if either an
  1006. * async dio is going to do it in the future or an end_io after an
  1007. * error has already done it.
  1008. */
  1009. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1010. rw_level = -1;
  1011. have_alloc_sem = 0;
  1012. }
  1013. out:
  1014. if (have_alloc_sem)
  1015. up_read(&inode->i_alloc_sem);
  1016. if (rw_level != -1)
  1017. ocfs2_rw_unlock(inode, rw_level);
  1018. mutex_unlock(&inode->i_mutex);
  1019. mlog_exit(ret);
  1020. return ret;
  1021. }
  1022. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1023. struct file *out,
  1024. loff_t *ppos,
  1025. size_t len,
  1026. unsigned int flags)
  1027. {
  1028. int ret;
  1029. struct inode *inode = out->f_path.dentry->d_inode;
  1030. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1031. (unsigned int)len,
  1032. out->f_path.dentry->d_name.len,
  1033. out->f_path.dentry->d_name.name);
  1034. inode_double_lock(inode, pipe->inode);
  1035. ret = ocfs2_rw_lock(inode, 1);
  1036. if (ret < 0) {
  1037. mlog_errno(ret);
  1038. goto out;
  1039. }
  1040. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0);
  1041. if (ret < 0) {
  1042. mlog_errno(ret);
  1043. goto out_unlock;
  1044. }
  1045. /* ok, we're done with i_size and alloc work */
  1046. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1047. out_unlock:
  1048. ocfs2_rw_unlock(inode, 1);
  1049. out:
  1050. inode_double_unlock(inode, pipe->inode);
  1051. mlog_exit(ret);
  1052. return ret;
  1053. }
  1054. static ssize_t ocfs2_file_splice_read(struct file *in,
  1055. loff_t *ppos,
  1056. struct pipe_inode_info *pipe,
  1057. size_t len,
  1058. unsigned int flags)
  1059. {
  1060. int ret = 0;
  1061. struct inode *inode = in->f_path.dentry->d_inode;
  1062. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1063. (unsigned int)len,
  1064. in->f_path.dentry->d_name.len,
  1065. in->f_path.dentry->d_name.name);
  1066. /*
  1067. * See the comment in ocfs2_file_aio_read()
  1068. */
  1069. ret = ocfs2_meta_lock(inode, NULL, 0);
  1070. if (ret < 0) {
  1071. mlog_errno(ret);
  1072. goto bail;
  1073. }
  1074. ocfs2_meta_unlock(inode, 0);
  1075. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1076. bail:
  1077. mlog_exit(ret);
  1078. return ret;
  1079. }
  1080. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1081. const struct iovec *iov,
  1082. unsigned long nr_segs,
  1083. loff_t pos)
  1084. {
  1085. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1086. struct file *filp = iocb->ki_filp;
  1087. struct inode *inode = filp->f_path.dentry->d_inode;
  1088. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1089. (unsigned int)nr_segs,
  1090. filp->f_path.dentry->d_name.len,
  1091. filp->f_path.dentry->d_name.name);
  1092. if (!inode) {
  1093. ret = -EINVAL;
  1094. mlog_errno(ret);
  1095. goto bail;
  1096. }
  1097. /*
  1098. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1099. * need locks to protect pending reads from racing with truncate.
  1100. */
  1101. if (filp->f_flags & O_DIRECT) {
  1102. down_read(&inode->i_alloc_sem);
  1103. have_alloc_sem = 1;
  1104. ret = ocfs2_rw_lock(inode, 0);
  1105. if (ret < 0) {
  1106. mlog_errno(ret);
  1107. goto bail;
  1108. }
  1109. rw_level = 0;
  1110. /* communicate with ocfs2_dio_end_io */
  1111. ocfs2_iocb_set_rw_locked(iocb);
  1112. }
  1113. /*
  1114. * We're fine letting folks race truncates and extending
  1115. * writes with read across the cluster, just like they can
  1116. * locally. Hence no rw_lock during read.
  1117. *
  1118. * Take and drop the meta data lock to update inode fields
  1119. * like i_size. This allows the checks down below
  1120. * generic_file_aio_read() a chance of actually working.
  1121. */
  1122. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1123. if (ret < 0) {
  1124. mlog_errno(ret);
  1125. goto bail;
  1126. }
  1127. ocfs2_meta_unlock(inode, lock_level);
  1128. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1129. if (ret == -EINVAL)
  1130. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1131. /* buffered aio wouldn't have proper lock coverage today */
  1132. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1133. /* see ocfs2_file_aio_write */
  1134. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1135. rw_level = -1;
  1136. have_alloc_sem = 0;
  1137. }
  1138. bail:
  1139. if (have_alloc_sem)
  1140. up_read(&inode->i_alloc_sem);
  1141. if (rw_level != -1)
  1142. ocfs2_rw_unlock(inode, rw_level);
  1143. mlog_exit(ret);
  1144. return ret;
  1145. }
  1146. struct inode_operations ocfs2_file_iops = {
  1147. .setattr = ocfs2_setattr,
  1148. .getattr = ocfs2_getattr,
  1149. .permission = ocfs2_permission,
  1150. };
  1151. struct inode_operations ocfs2_special_file_iops = {
  1152. .setattr = ocfs2_setattr,
  1153. .getattr = ocfs2_getattr,
  1154. .permission = ocfs2_permission,
  1155. };
  1156. const struct file_operations ocfs2_fops = {
  1157. .read = do_sync_read,
  1158. .write = do_sync_write,
  1159. .sendfile = generic_file_sendfile,
  1160. .mmap = ocfs2_mmap,
  1161. .fsync = ocfs2_sync_file,
  1162. .release = ocfs2_file_release,
  1163. .open = ocfs2_file_open,
  1164. .aio_read = ocfs2_file_aio_read,
  1165. .aio_write = ocfs2_file_aio_write,
  1166. .ioctl = ocfs2_ioctl,
  1167. .splice_read = ocfs2_file_splice_read,
  1168. .splice_write = ocfs2_file_splice_write,
  1169. };
  1170. const struct file_operations ocfs2_dops = {
  1171. .read = generic_read_dir,
  1172. .readdir = ocfs2_readdir,
  1173. .fsync = ocfs2_sync_file,
  1174. .ioctl = ocfs2_ioctl,
  1175. };