file.c 30 KB

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