file.c 34 KB

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