file.c 30 KB

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