file.c 29 KB

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