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/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 "ioctl.h"
  44. #include "journal.h"
  45. #include "mmap.h"
  46. #include "suballoc.h"
  47. #include "super.h"
  48. #include "buffer_head_io.h"
  49. static int ocfs2_sync_inode(struct inode *inode)
  50. {
  51. filemap_fdatawrite(inode->i_mapping);
  52. return sync_mapping_buffers(inode->i_mapping);
  53. }
  54. static int ocfs2_file_open(struct inode *inode, struct file *file)
  55. {
  56. int status;
  57. int mode = file->f_flags;
  58. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  59. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  60. file->f_dentry->d_name.len, file->f_dentry->d_name.name);
  61. spin_lock(&oi->ip_lock);
  62. /* Check that the inode hasn't been wiped from disk by another
  63. * node. If it hasn't then we're safe as long as we hold the
  64. * spin lock until our increment of open count. */
  65. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  66. spin_unlock(&oi->ip_lock);
  67. status = -ENOENT;
  68. goto leave;
  69. }
  70. if (mode & O_DIRECT)
  71. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  72. oi->ip_open_count++;
  73. spin_unlock(&oi->ip_lock);
  74. status = 0;
  75. leave:
  76. mlog_exit(status);
  77. return status;
  78. }
  79. static int ocfs2_file_release(struct inode *inode, struct file *file)
  80. {
  81. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  82. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  83. file->f_dentry->d_name.len,
  84. file->f_dentry->d_name.name);
  85. spin_lock(&oi->ip_lock);
  86. if (!--oi->ip_open_count)
  87. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  88. spin_unlock(&oi->ip_lock);
  89. mlog_exit(0);
  90. return 0;
  91. }
  92. static int ocfs2_sync_file(struct file *file,
  93. struct dentry *dentry,
  94. int datasync)
  95. {
  96. int err = 0;
  97. journal_t *journal;
  98. struct inode *inode = dentry->d_inode;
  99. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  100. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  101. dentry->d_name.len, dentry->d_name.name);
  102. err = ocfs2_sync_inode(dentry->d_inode);
  103. if (err)
  104. goto bail;
  105. journal = osb->journal->j_journal;
  106. err = journal_force_commit(journal);
  107. bail:
  108. mlog_exit(err);
  109. return (err < 0) ? -EIO : 0;
  110. }
  111. int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle,
  112. struct inode *inode,
  113. struct buffer_head *fe_bh,
  114. u64 new_i_size)
  115. {
  116. int status;
  117. mlog_entry_void();
  118. i_size_write(inode, new_i_size);
  119. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  120. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  121. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  122. if (status < 0) {
  123. mlog_errno(status);
  124. goto bail;
  125. }
  126. bail:
  127. mlog_exit(status);
  128. return status;
  129. }
  130. static int ocfs2_simple_size_update(struct inode *inode,
  131. struct buffer_head *di_bh,
  132. u64 new_i_size)
  133. {
  134. int ret;
  135. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  136. struct ocfs2_journal_handle *handle = NULL;
  137. handle = ocfs2_start_trans(osb, NULL,
  138. 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(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. struct ocfs2_journal_handle *handle;
  159. mlog_entry_void();
  160. /* TODO: This needs to actually orphan the inode in this
  161. * transaction. */
  162. handle = ocfs2_start_trans(osb, NULL, 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(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. struct ocfs2_journal_handle *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. struct ocfs2_journal_handle *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. handle = ocfs2_alloc_handle(osb);
  393. if (handle == NULL) {
  394. status = -ENOMEM;
  395. mlog_errno(status);
  396. goto leave;
  397. }
  398. num_free_extents = ocfs2_num_free_extents(osb,
  399. inode,
  400. fe);
  401. if (num_free_extents < 0) {
  402. status = num_free_extents;
  403. mlog_errno(status);
  404. goto leave;
  405. }
  406. if (!num_free_extents) {
  407. status = ocfs2_reserve_new_metadata(osb,
  408. handle,
  409. fe,
  410. &meta_ac);
  411. if (status < 0) {
  412. if (status != -ENOSPC)
  413. mlog_errno(status);
  414. goto leave;
  415. }
  416. }
  417. status = ocfs2_reserve_clusters(osb,
  418. handle,
  419. clusters_to_add,
  420. &data_ac);
  421. if (status < 0) {
  422. if (status != -ENOSPC)
  423. mlog_errno(status);
  424. goto leave;
  425. }
  426. /* blocks peope in read/write from reading our allocation
  427. * until we're done changing it. We depend on i_mutex to block
  428. * other extend/truncate calls while we're here. Ordering wrt
  429. * start_trans is important here -- always do it before! */
  430. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  431. drop_alloc_sem = 1;
  432. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  433. handle = ocfs2_start_trans(osb, handle, credits);
  434. if (IS_ERR(handle)) {
  435. status = PTR_ERR(handle);
  436. handle = NULL;
  437. mlog_errno(status);
  438. goto leave;
  439. }
  440. restarted_transaction:
  441. /* reserve a write to the file entry early on - that we if we
  442. * run out of credits in the allocation path, we can still
  443. * update i_size. */
  444. status = ocfs2_journal_access(handle, inode, bh,
  445. OCFS2_JOURNAL_ACCESS_WRITE);
  446. if (status < 0) {
  447. mlog_errno(status);
  448. goto leave;
  449. }
  450. prev_clusters = OCFS2_I(inode)->ip_clusters;
  451. status = ocfs2_do_extend_allocation(osb,
  452. inode,
  453. clusters_to_add,
  454. bh,
  455. handle,
  456. data_ac,
  457. meta_ac,
  458. &why);
  459. if ((status < 0) && (status != -EAGAIN)) {
  460. if (status != -ENOSPC)
  461. mlog_errno(status);
  462. goto leave;
  463. }
  464. status = ocfs2_journal_dirty(handle, bh);
  465. if (status < 0) {
  466. mlog_errno(status);
  467. goto leave;
  468. }
  469. spin_lock(&OCFS2_I(inode)->ip_lock);
  470. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  471. spin_unlock(&OCFS2_I(inode)->ip_lock);
  472. if (why != RESTART_NONE && clusters_to_add) {
  473. if (why == RESTART_META) {
  474. mlog(0, "restarting function.\n");
  475. restart_func = 1;
  476. } else {
  477. BUG_ON(why != RESTART_TRANS);
  478. mlog(0, "restarting transaction.\n");
  479. /* TODO: This can be more intelligent. */
  480. credits = ocfs2_calc_extend_credits(osb->sb,
  481. fe,
  482. clusters_to_add);
  483. status = ocfs2_extend_trans(handle, credits);
  484. if (status < 0) {
  485. /* handle still has to be committed at
  486. * this point. */
  487. status = -ENOMEM;
  488. mlog_errno(status);
  489. goto leave;
  490. }
  491. goto restarted_transaction;
  492. }
  493. }
  494. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  495. fe->i_clusters, (unsigned long long)fe->i_size);
  496. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  497. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  498. leave:
  499. if (drop_alloc_sem) {
  500. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  501. drop_alloc_sem = 0;
  502. }
  503. if (handle) {
  504. ocfs2_commit_trans(handle);
  505. handle = NULL;
  506. }
  507. if (data_ac) {
  508. ocfs2_free_alloc_context(data_ac);
  509. data_ac = NULL;
  510. }
  511. if (meta_ac) {
  512. ocfs2_free_alloc_context(meta_ac);
  513. meta_ac = NULL;
  514. }
  515. if ((!status) && restart_func) {
  516. restart_func = 0;
  517. goto restart_all;
  518. }
  519. if (bh) {
  520. brelse(bh);
  521. bh = NULL;
  522. }
  523. mlog_exit(status);
  524. return status;
  525. }
  526. /* Some parts of this taken from generic_cont_expand, which turned out
  527. * to be too fragile to do exactly what we need without us having to
  528. * worry about recursive locking in ->prepare_write() and
  529. * ->commit_write(). */
  530. static int ocfs2_write_zero_page(struct inode *inode,
  531. u64 size)
  532. {
  533. struct address_space *mapping = inode->i_mapping;
  534. struct page *page;
  535. unsigned long index;
  536. unsigned int offset;
  537. struct ocfs2_journal_handle *handle = NULL;
  538. int ret;
  539. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  540. /* ugh. in prepare/commit_write, if from==to==start of block, we
  541. ** skip the prepare. make sure we never send an offset for the start
  542. ** of a block
  543. */
  544. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  545. offset++;
  546. }
  547. index = size >> PAGE_CACHE_SHIFT;
  548. page = grab_cache_page(mapping, index);
  549. if (!page) {
  550. ret = -ENOMEM;
  551. mlog_errno(ret);
  552. goto out;
  553. }
  554. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  555. if (ret < 0) {
  556. mlog_errno(ret);
  557. goto out_unlock;
  558. }
  559. if (ocfs2_should_order_data(inode)) {
  560. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  561. offset);
  562. if (IS_ERR(handle)) {
  563. ret = PTR_ERR(handle);
  564. handle = NULL;
  565. goto out_unlock;
  566. }
  567. }
  568. /* must not update i_size! */
  569. ret = block_commit_write(page, offset, offset);
  570. if (ret < 0)
  571. mlog_errno(ret);
  572. else
  573. ret = 0;
  574. if (handle)
  575. ocfs2_commit_trans(handle);
  576. out_unlock:
  577. unlock_page(page);
  578. page_cache_release(page);
  579. out:
  580. return ret;
  581. }
  582. static int ocfs2_zero_extend(struct inode *inode,
  583. u64 zero_to_size)
  584. {
  585. int ret = 0;
  586. u64 start_off;
  587. struct super_block *sb = inode->i_sb;
  588. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  589. while (start_off < zero_to_size) {
  590. ret = ocfs2_write_zero_page(inode, start_off);
  591. if (ret < 0) {
  592. mlog_errno(ret);
  593. goto out;
  594. }
  595. start_off += sb->s_blocksize;
  596. }
  597. out:
  598. return ret;
  599. }
  600. /*
  601. * A tail_to_skip value > 0 indicates that we're being called from
  602. * ocfs2_file_aio_write(). This has the following implications:
  603. *
  604. * - we don't want to update i_size
  605. * - di_bh will be NULL, which is fine because it's only used in the
  606. * case where we want to update i_size.
  607. * - ocfs2_zero_extend() will then only be filling the hole created
  608. * between i_size and the start of the write.
  609. */
  610. static int ocfs2_extend_file(struct inode *inode,
  611. struct buffer_head *di_bh,
  612. u64 new_i_size,
  613. size_t tail_to_skip)
  614. {
  615. int ret = 0;
  616. u32 clusters_to_add;
  617. BUG_ON(!tail_to_skip && !di_bh);
  618. /* setattr sometimes calls us like this. */
  619. if (new_i_size == 0)
  620. goto out;
  621. if (i_size_read(inode) == new_i_size)
  622. goto out;
  623. BUG_ON(new_i_size < i_size_read(inode));
  624. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  625. OCFS2_I(inode)->ip_clusters;
  626. if (clusters_to_add) {
  627. /*
  628. * protect the pages that ocfs2_zero_extend is going to
  629. * be pulling into the page cache.. we do this before the
  630. * metadata extend so that we don't get into the situation
  631. * where we've extended the metadata but can't get the data
  632. * lock to zero.
  633. */
  634. ret = ocfs2_data_lock(inode, 1);
  635. if (ret < 0) {
  636. mlog_errno(ret);
  637. goto out;
  638. }
  639. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  640. if (ret < 0) {
  641. mlog_errno(ret);
  642. goto out_unlock;
  643. }
  644. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  645. if (ret < 0) {
  646. mlog_errno(ret);
  647. goto out_unlock;
  648. }
  649. }
  650. if (!tail_to_skip) {
  651. /* We're being called from ocfs2_setattr() which wants
  652. * us to update i_size */
  653. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  654. if (ret < 0)
  655. mlog_errno(ret);
  656. }
  657. out_unlock:
  658. if (clusters_to_add) /* this is the only case in which we lock */
  659. ocfs2_data_unlock(inode, 1);
  660. out:
  661. return ret;
  662. }
  663. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  664. {
  665. int status = 0, size_change;
  666. struct inode *inode = dentry->d_inode;
  667. struct super_block *sb = inode->i_sb;
  668. struct ocfs2_super *osb = OCFS2_SB(sb);
  669. struct buffer_head *bh = NULL;
  670. struct ocfs2_journal_handle *handle = NULL;
  671. mlog_entry("(0x%p, '%.*s')\n", dentry,
  672. dentry->d_name.len, dentry->d_name.name);
  673. if (attr->ia_valid & ATTR_MODE)
  674. mlog(0, "mode change: %d\n", attr->ia_mode);
  675. if (attr->ia_valid & ATTR_UID)
  676. mlog(0, "uid change: %d\n", attr->ia_uid);
  677. if (attr->ia_valid & ATTR_GID)
  678. mlog(0, "gid change: %d\n", attr->ia_gid);
  679. if (attr->ia_valid & ATTR_SIZE)
  680. mlog(0, "size change...\n");
  681. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  682. mlog(0, "time change...\n");
  683. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  684. | ATTR_GID | ATTR_UID | ATTR_MODE)
  685. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  686. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  687. return 0;
  688. }
  689. status = inode_change_ok(inode, attr);
  690. if (status)
  691. return status;
  692. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  693. if (size_change) {
  694. status = ocfs2_rw_lock(inode, 1);
  695. if (status < 0) {
  696. mlog_errno(status);
  697. goto bail;
  698. }
  699. }
  700. status = ocfs2_meta_lock(inode, NULL, &bh, 1);
  701. if (status < 0) {
  702. if (status != -ENOENT)
  703. mlog_errno(status);
  704. goto bail_unlock_rw;
  705. }
  706. if (size_change && attr->ia_size != i_size_read(inode)) {
  707. if (i_size_read(inode) > attr->ia_size)
  708. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  709. else
  710. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  711. if (status < 0) {
  712. if (status != -ENOSPC)
  713. mlog_errno(status);
  714. status = -ENOSPC;
  715. goto bail_unlock;
  716. }
  717. }
  718. handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
  719. if (IS_ERR(handle)) {
  720. status = PTR_ERR(handle);
  721. mlog_errno(status);
  722. goto bail_unlock;
  723. }
  724. status = inode_setattr(inode, attr);
  725. if (status < 0) {
  726. mlog_errno(status);
  727. goto bail_commit;
  728. }
  729. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  730. if (status < 0)
  731. mlog_errno(status);
  732. bail_commit:
  733. ocfs2_commit_trans(handle);
  734. bail_unlock:
  735. ocfs2_meta_unlock(inode, 1);
  736. bail_unlock_rw:
  737. if (size_change)
  738. ocfs2_rw_unlock(inode, 1);
  739. bail:
  740. if (bh)
  741. brelse(bh);
  742. mlog_exit(status);
  743. return status;
  744. }
  745. int ocfs2_getattr(struct vfsmount *mnt,
  746. struct dentry *dentry,
  747. struct kstat *stat)
  748. {
  749. struct inode *inode = dentry->d_inode;
  750. struct super_block *sb = dentry->d_inode->i_sb;
  751. struct ocfs2_super *osb = sb->s_fs_info;
  752. int err;
  753. mlog_entry_void();
  754. err = ocfs2_inode_revalidate(dentry);
  755. if (err) {
  756. if (err != -ENOENT)
  757. mlog_errno(err);
  758. goto bail;
  759. }
  760. generic_fillattr(inode, stat);
  761. /* We set the blksize from the cluster size for performance */
  762. stat->blksize = osb->s_clustersize;
  763. bail:
  764. mlog_exit(err);
  765. return err;
  766. }
  767. static int ocfs2_write_remove_suid(struct inode *inode)
  768. {
  769. int ret;
  770. struct buffer_head *bh = NULL;
  771. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  772. struct ocfs2_journal_handle *handle;
  773. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  774. struct ocfs2_dinode *di;
  775. mlog_entry("(Inode %llu, mode 0%o)\n",
  776. (unsigned long long)oi->ip_blkno, inode->i_mode);
  777. handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
  778. if (handle == NULL) {
  779. ret = -ENOMEM;
  780. mlog_errno(ret);
  781. goto out;
  782. }
  783. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  784. if (ret < 0) {
  785. mlog_errno(ret);
  786. goto out_trans;
  787. }
  788. ret = ocfs2_journal_access(handle, inode, bh,
  789. OCFS2_JOURNAL_ACCESS_WRITE);
  790. if (ret < 0) {
  791. mlog_errno(ret);
  792. goto out_bh;
  793. }
  794. inode->i_mode &= ~S_ISUID;
  795. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  796. inode->i_mode &= ~S_ISGID;
  797. di = (struct ocfs2_dinode *) bh->b_data;
  798. di->i_mode = cpu_to_le16(inode->i_mode);
  799. ret = ocfs2_journal_dirty(handle, bh);
  800. if (ret < 0)
  801. mlog_errno(ret);
  802. out_bh:
  803. brelse(bh);
  804. out_trans:
  805. ocfs2_commit_trans(handle);
  806. out:
  807. mlog_exit(ret);
  808. return ret;
  809. }
  810. static inline int ocfs2_write_should_remove_suid(struct inode *inode)
  811. {
  812. mode_t mode = inode->i_mode;
  813. if (!capable(CAP_FSETID)) {
  814. if (unlikely(mode & S_ISUID))
  815. return 1;
  816. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  817. return 1;
  818. }
  819. return 0;
  820. }
  821. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  822. const struct iovec *iov,
  823. unsigned long nr_segs,
  824. loff_t pos)
  825. {
  826. int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
  827. u32 clusters;
  828. struct file *filp = iocb->ki_filp;
  829. struct inode *inode = filp->f_dentry->d_inode;
  830. loff_t newsize, saved_pos;
  831. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  832. (unsigned int)nr_segs,
  833. filp->f_dentry->d_name.len,
  834. filp->f_dentry->d_name.name);
  835. /* happy write of zero bytes */
  836. if (iocb->ki_left == 0)
  837. return 0;
  838. if (!inode) {
  839. mlog(0, "bad inode\n");
  840. return -EIO;
  841. }
  842. mutex_lock(&inode->i_mutex);
  843. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  844. if (filp->f_flags & O_DIRECT) {
  845. have_alloc_sem = 1;
  846. down_read(&inode->i_alloc_sem);
  847. }
  848. /* concurrent O_DIRECT writes are allowed */
  849. rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
  850. ret = ocfs2_rw_lock(inode, rw_level);
  851. if (ret < 0) {
  852. rw_level = -1;
  853. mlog_errno(ret);
  854. goto out;
  855. }
  856. /*
  857. * We sample i_size under a read level meta lock to see if our write
  858. * is extending the file, if it is we back off and get a write level
  859. * meta lock.
  860. */
  861. meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
  862. for(;;) {
  863. ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level);
  864. if (ret < 0) {
  865. meta_level = -1;
  866. mlog_errno(ret);
  867. goto out;
  868. }
  869. /* Clear suid / sgid if necessary. We do this here
  870. * instead of later in the write path because
  871. * remove_suid() calls ->setattr without any hint that
  872. * we may have already done our cluster locking. Since
  873. * ocfs2_setattr() *must* take cluster locks to
  874. * proceeed, this will lead us to recursively lock the
  875. * inode. There's also the dinode i_size state which
  876. * can be lost via setattr during extending writes (we
  877. * set inode->i_size at the end of a write. */
  878. if (ocfs2_write_should_remove_suid(inode)) {
  879. if (meta_level == 0) {
  880. ocfs2_meta_unlock(inode, meta_level);
  881. meta_level = 1;
  882. continue;
  883. }
  884. ret = ocfs2_write_remove_suid(inode);
  885. if (ret < 0) {
  886. mlog_errno(ret);
  887. goto out;
  888. }
  889. }
  890. /* work on a copy of ppos until we're sure that we won't have
  891. * to recalculate it due to relocking. */
  892. if (filp->f_flags & O_APPEND) {
  893. saved_pos = i_size_read(inode);
  894. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  895. } else {
  896. saved_pos = iocb->ki_pos;
  897. }
  898. newsize = iocb->ki_left + saved_pos;
  899. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  900. (long long) saved_pos, (long long) newsize,
  901. (long long) i_size_read(inode));
  902. /* No need for a higher level metadata lock if we're
  903. * never going past i_size. */
  904. if (newsize <= i_size_read(inode))
  905. break;
  906. if (meta_level == 0) {
  907. ocfs2_meta_unlock(inode, meta_level);
  908. meta_level = 1;
  909. continue;
  910. }
  911. spin_lock(&OCFS2_I(inode)->ip_lock);
  912. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  913. OCFS2_I(inode)->ip_clusters;
  914. spin_unlock(&OCFS2_I(inode)->ip_lock);
  915. mlog(0, "Writing at EOF, may need more allocation: "
  916. "i_size = %lld, newsize = %lld, need %u clusters\n",
  917. (long long) i_size_read(inode), (long long) newsize,
  918. clusters);
  919. /* We only want to continue the rest of this loop if
  920. * our extend will actually require more
  921. * allocation. */
  922. if (!clusters)
  923. break;
  924. ret = ocfs2_extend_file(inode, NULL, newsize, iocb->ki_left);
  925. if (ret < 0) {
  926. if (ret != -ENOSPC)
  927. mlog_errno(ret);
  928. goto out;
  929. }
  930. break;
  931. }
  932. /* ok, we're done with i_size and alloc work */
  933. iocb->ki_pos = saved_pos;
  934. ocfs2_meta_unlock(inode, meta_level);
  935. meta_level = -1;
  936. /* communicate with ocfs2_dio_end_io */
  937. ocfs2_iocb_set_rw_locked(iocb);
  938. ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
  939. /* buffered aio wouldn't have proper lock coverage today */
  940. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  941. /*
  942. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  943. * function pointer which is called when o_direct io completes so that
  944. * it can unlock our rw lock. (it's the clustered equivalent of
  945. * i_alloc_sem; protects truncate from racing with pending ios).
  946. * Unfortunately there are error cases which call end_io and others
  947. * that don't. so we don't have to unlock the rw_lock if either an
  948. * async dio is going to do it in the future or an end_io after an
  949. * error has already done it.
  950. */
  951. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  952. rw_level = -1;
  953. have_alloc_sem = 0;
  954. }
  955. out:
  956. if (meta_level != -1)
  957. ocfs2_meta_unlock(inode, meta_level);
  958. if (have_alloc_sem)
  959. up_read(&inode->i_alloc_sem);
  960. if (rw_level != -1)
  961. ocfs2_rw_unlock(inode, rw_level);
  962. mutex_unlock(&inode->i_mutex);
  963. mlog_exit(ret);
  964. return ret;
  965. }
  966. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  967. const struct iovec *iov,
  968. unsigned long nr_segs,
  969. loff_t pos)
  970. {
  971. int ret = 0, rw_level = -1, have_alloc_sem = 0;
  972. struct file *filp = iocb->ki_filp;
  973. struct inode *inode = filp->f_dentry->d_inode;
  974. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  975. (unsigned int)nr_segs,
  976. filp->f_dentry->d_name.len,
  977. filp->f_dentry->d_name.name);
  978. if (!inode) {
  979. ret = -EINVAL;
  980. mlog_errno(ret);
  981. goto bail;
  982. }
  983. /*
  984. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  985. * need locks to protect pending reads from racing with truncate.
  986. */
  987. if (filp->f_flags & O_DIRECT) {
  988. down_read(&inode->i_alloc_sem);
  989. have_alloc_sem = 1;
  990. ret = ocfs2_rw_lock(inode, 0);
  991. if (ret < 0) {
  992. mlog_errno(ret);
  993. goto bail;
  994. }
  995. rw_level = 0;
  996. /* communicate with ocfs2_dio_end_io */
  997. ocfs2_iocb_set_rw_locked(iocb);
  998. }
  999. /*
  1000. * We're fine letting folks race truncates and extending
  1001. * writes with read across the cluster, just like they can
  1002. * locally. Hence no rw_lock during read.
  1003. *
  1004. * Take and drop the meta data lock to update inode fields
  1005. * like i_size. This allows the checks down below
  1006. * generic_file_aio_read() a chance of actually working.
  1007. */
  1008. ret = ocfs2_meta_lock(inode, NULL, NULL, 0);
  1009. if (ret < 0) {
  1010. mlog_errno(ret);
  1011. goto bail;
  1012. }
  1013. ocfs2_meta_unlock(inode, 0);
  1014. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1015. if (ret == -EINVAL)
  1016. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1017. /* buffered aio wouldn't have proper lock coverage today */
  1018. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1019. /* see ocfs2_file_aio_write */
  1020. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1021. rw_level = -1;
  1022. have_alloc_sem = 0;
  1023. }
  1024. bail:
  1025. if (have_alloc_sem)
  1026. up_read(&inode->i_alloc_sem);
  1027. if (rw_level != -1)
  1028. ocfs2_rw_unlock(inode, rw_level);
  1029. mlog_exit(ret);
  1030. return ret;
  1031. }
  1032. struct inode_operations ocfs2_file_iops = {
  1033. .setattr = ocfs2_setattr,
  1034. .getattr = ocfs2_getattr,
  1035. };
  1036. struct inode_operations ocfs2_special_file_iops = {
  1037. .setattr = ocfs2_setattr,
  1038. .getattr = ocfs2_getattr,
  1039. };
  1040. const struct file_operations ocfs2_fops = {
  1041. .read = do_sync_read,
  1042. .write = do_sync_write,
  1043. .sendfile = generic_file_sendfile,
  1044. .mmap = ocfs2_mmap,
  1045. .fsync = ocfs2_sync_file,
  1046. .release = ocfs2_file_release,
  1047. .open = ocfs2_file_open,
  1048. .aio_read = ocfs2_file_aio_read,
  1049. .aio_write = ocfs2_file_aio_write,
  1050. .ioctl = ocfs2_ioctl,
  1051. };
  1052. const struct file_operations ocfs2_dops = {
  1053. .read = generic_read_dir,
  1054. .readdir = ocfs2_readdir,
  1055. .fsync = ocfs2_sync_file,
  1056. .ioctl = ocfs2_ioctl,
  1057. };