file.c 34 KB

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