file.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 ->prepare_write() and
  528. * ->commit_write(). */
  529. static int ocfs2_write_zero_page(struct inode *inode,
  530. u64 size)
  531. {
  532. struct address_space *mapping = inode->i_mapping;
  533. struct page *page;
  534. unsigned long index;
  535. unsigned int offset;
  536. struct ocfs2_journal_handle *handle = NULL;
  537. int ret;
  538. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  539. /* ugh. in prepare/commit_write, if from==to==start of block, we
  540. ** skip the prepare. make sure we never send an offset for the start
  541. ** of a block
  542. */
  543. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  544. offset++;
  545. }
  546. index = size >> PAGE_CACHE_SHIFT;
  547. page = grab_cache_page(mapping, index);
  548. if (!page) {
  549. ret = -ENOMEM;
  550. mlog_errno(ret);
  551. goto out;
  552. }
  553. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  554. if (ret < 0) {
  555. mlog_errno(ret);
  556. goto out_unlock;
  557. }
  558. if (ocfs2_should_order_data(inode)) {
  559. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  560. offset);
  561. if (IS_ERR(handle)) {
  562. ret = PTR_ERR(handle);
  563. handle = NULL;
  564. goto out_unlock;
  565. }
  566. }
  567. /* must not update i_size! */
  568. ret = block_commit_write(page, offset, offset);
  569. if (ret < 0)
  570. mlog_errno(ret);
  571. else
  572. ret = 0;
  573. if (handle)
  574. ocfs2_commit_trans(handle);
  575. out_unlock:
  576. unlock_page(page);
  577. page_cache_release(page);
  578. out:
  579. return ret;
  580. }
  581. static int ocfs2_zero_extend(struct inode *inode,
  582. u64 zero_to_size)
  583. {
  584. int ret = 0;
  585. u64 start_off;
  586. struct super_block *sb = inode->i_sb;
  587. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  588. while (start_off < zero_to_size) {
  589. ret = ocfs2_write_zero_page(inode, start_off);
  590. if (ret < 0) {
  591. mlog_errno(ret);
  592. goto out;
  593. }
  594. start_off += sb->s_blocksize;
  595. }
  596. out:
  597. return ret;
  598. }
  599. /*
  600. * A tail_to_skip value > 0 indicates that we're being called from
  601. * ocfs2_file_aio_write(). This has the following implications:
  602. *
  603. * - we don't want to update i_size
  604. * - di_bh will be NULL, which is fine because it's only used in the
  605. * case where we want to update i_size.
  606. * - ocfs2_zero_extend() will then only be filling the hole created
  607. * between i_size and the start of the write.
  608. */
  609. static int ocfs2_extend_file(struct inode *inode,
  610. struct buffer_head *di_bh,
  611. u64 new_i_size,
  612. size_t tail_to_skip)
  613. {
  614. int ret = 0;
  615. u32 clusters_to_add;
  616. BUG_ON(!tail_to_skip && !di_bh);
  617. /* setattr sometimes calls us like this. */
  618. if (new_i_size == 0)
  619. goto out;
  620. if (i_size_read(inode) == new_i_size)
  621. goto out;
  622. BUG_ON(new_i_size < i_size_read(inode));
  623. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  624. OCFS2_I(inode)->ip_clusters;
  625. if (clusters_to_add) {
  626. /*
  627. * protect the pages that ocfs2_zero_extend is going to
  628. * be pulling into the page cache.. we do this before the
  629. * metadata extend so that we don't get into the situation
  630. * where we've extended the metadata but can't get the data
  631. * lock to zero.
  632. */
  633. ret = ocfs2_data_lock(inode, 1);
  634. if (ret < 0) {
  635. mlog_errno(ret);
  636. goto out;
  637. }
  638. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  639. if (ret < 0) {
  640. mlog_errno(ret);
  641. goto out_unlock;
  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. }
  649. if (!tail_to_skip) {
  650. /* We're being called from ocfs2_setattr() which wants
  651. * us to update i_size */
  652. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  653. if (ret < 0)
  654. mlog_errno(ret);
  655. }
  656. out_unlock:
  657. if (clusters_to_add) /* this is the only case in which we lock */
  658. ocfs2_data_unlock(inode, 1);
  659. out:
  660. return ret;
  661. }
  662. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  663. {
  664. int status = 0, size_change;
  665. struct inode *inode = dentry->d_inode;
  666. struct super_block *sb = inode->i_sb;
  667. struct ocfs2_super *osb = OCFS2_SB(sb);
  668. struct buffer_head *bh = NULL;
  669. struct ocfs2_journal_handle *handle = NULL;
  670. mlog_entry("(0x%p, '%.*s')\n", dentry,
  671. dentry->d_name.len, dentry->d_name.name);
  672. if (attr->ia_valid & ATTR_MODE)
  673. mlog(0, "mode change: %d\n", attr->ia_mode);
  674. if (attr->ia_valid & ATTR_UID)
  675. mlog(0, "uid change: %d\n", attr->ia_uid);
  676. if (attr->ia_valid & ATTR_GID)
  677. mlog(0, "gid change: %d\n", attr->ia_gid);
  678. if (attr->ia_valid & ATTR_SIZE)
  679. mlog(0, "size change...\n");
  680. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  681. mlog(0, "time change...\n");
  682. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  683. | ATTR_GID | ATTR_UID | ATTR_MODE)
  684. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  685. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  686. return 0;
  687. }
  688. status = inode_change_ok(inode, attr);
  689. if (status)
  690. return status;
  691. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  692. if (size_change) {
  693. status = ocfs2_rw_lock(inode, 1);
  694. if (status < 0) {
  695. mlog_errno(status);
  696. goto bail;
  697. }
  698. }
  699. status = ocfs2_meta_lock(inode, NULL, &bh, 1);
  700. if (status < 0) {
  701. if (status != -ENOENT)
  702. mlog_errno(status);
  703. goto bail_unlock_rw;
  704. }
  705. if (size_change && attr->ia_size != i_size_read(inode)) {
  706. if (i_size_read(inode) > attr->ia_size)
  707. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  708. else
  709. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  710. if (status < 0) {
  711. if (status != -ENOSPC)
  712. mlog_errno(status);
  713. status = -ENOSPC;
  714. goto bail_unlock;
  715. }
  716. }
  717. handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
  718. if (IS_ERR(handle)) {
  719. status = PTR_ERR(handle);
  720. mlog_errno(status);
  721. goto bail_unlock;
  722. }
  723. status = inode_setattr(inode, attr);
  724. if (status < 0) {
  725. mlog_errno(status);
  726. goto bail_commit;
  727. }
  728. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  729. if (status < 0)
  730. mlog_errno(status);
  731. bail_commit:
  732. ocfs2_commit_trans(handle);
  733. bail_unlock:
  734. ocfs2_meta_unlock(inode, 1);
  735. bail_unlock_rw:
  736. if (size_change)
  737. ocfs2_rw_unlock(inode, 1);
  738. bail:
  739. if (bh)
  740. brelse(bh);
  741. mlog_exit(status);
  742. return status;
  743. }
  744. int ocfs2_getattr(struct vfsmount *mnt,
  745. struct dentry *dentry,
  746. struct kstat *stat)
  747. {
  748. struct inode *inode = dentry->d_inode;
  749. struct super_block *sb = dentry->d_inode->i_sb;
  750. struct ocfs2_super *osb = sb->s_fs_info;
  751. int err;
  752. mlog_entry_void();
  753. err = ocfs2_inode_revalidate(dentry);
  754. if (err) {
  755. if (err != -ENOENT)
  756. mlog_errno(err);
  757. goto bail;
  758. }
  759. generic_fillattr(inode, stat);
  760. /* We set the blksize from the cluster size for performance */
  761. stat->blksize = osb->s_clustersize;
  762. bail:
  763. mlog_exit(err);
  764. return err;
  765. }
  766. static int ocfs2_write_remove_suid(struct inode *inode)
  767. {
  768. int ret;
  769. struct buffer_head *bh = NULL;
  770. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  771. struct ocfs2_journal_handle *handle;
  772. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  773. struct ocfs2_dinode *di;
  774. mlog_entry("(Inode %llu, mode 0%o)\n",
  775. (unsigned long long)oi->ip_blkno, inode->i_mode);
  776. handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
  777. if (handle == NULL) {
  778. ret = -ENOMEM;
  779. mlog_errno(ret);
  780. goto out;
  781. }
  782. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  783. if (ret < 0) {
  784. mlog_errno(ret);
  785. goto out_trans;
  786. }
  787. ret = ocfs2_journal_access(handle, inode, bh,
  788. OCFS2_JOURNAL_ACCESS_WRITE);
  789. if (ret < 0) {
  790. mlog_errno(ret);
  791. goto out_bh;
  792. }
  793. inode->i_mode &= ~S_ISUID;
  794. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  795. inode->i_mode &= ~S_ISGID;
  796. di = (struct ocfs2_dinode *) bh->b_data;
  797. di->i_mode = cpu_to_le16(inode->i_mode);
  798. ret = ocfs2_journal_dirty(handle, bh);
  799. if (ret < 0)
  800. mlog_errno(ret);
  801. out_bh:
  802. brelse(bh);
  803. out_trans:
  804. ocfs2_commit_trans(handle);
  805. out:
  806. mlog_exit(ret);
  807. return ret;
  808. }
  809. static inline int ocfs2_write_should_remove_suid(struct inode *inode)
  810. {
  811. mode_t mode = inode->i_mode;
  812. if (!capable(CAP_FSETID)) {
  813. if (unlikely(mode & S_ISUID))
  814. return 1;
  815. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  816. return 1;
  817. }
  818. return 0;
  819. }
  820. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  821. const char __user *buf,
  822. size_t count,
  823. loff_t pos)
  824. {
  825. struct iovec local_iov = { .iov_base = (void __user *)buf,
  826. .iov_len = count };
  827. int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
  828. u32 clusters;
  829. struct file *filp = iocb->ki_filp;
  830. struct inode *inode = filp->f_dentry->d_inode;
  831. loff_t newsize, saved_pos;
  832. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
  833. (unsigned int)count,
  834. filp->f_dentry->d_name.len,
  835. filp->f_dentry->d_name.name);
  836. /* happy write of zero bytes */
  837. if (count == 0)
  838. return 0;
  839. if (!inode) {
  840. mlog(0, "bad inode\n");
  841. return -EIO;
  842. }
  843. mutex_lock(&inode->i_mutex);
  844. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  845. if (filp->f_flags & O_DIRECT) {
  846. have_alloc_sem = 1;
  847. down_read(&inode->i_alloc_sem);
  848. }
  849. /* concurrent O_DIRECT writes are allowed */
  850. rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
  851. ret = ocfs2_rw_lock(inode, rw_level);
  852. if (ret < 0) {
  853. rw_level = -1;
  854. mlog_errno(ret);
  855. goto out;
  856. }
  857. /*
  858. * We sample i_size under a read level meta lock to see if our write
  859. * is extending the file, if it is we back off and get a write level
  860. * meta lock.
  861. */
  862. meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
  863. for(;;) {
  864. ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level);
  865. if (ret < 0) {
  866. meta_level = -1;
  867. mlog_errno(ret);
  868. goto out;
  869. }
  870. /* Clear suid / sgid if necessary. We do this here
  871. * instead of later in the write path because
  872. * remove_suid() calls ->setattr without any hint that
  873. * we may have already done our cluster locking. Since
  874. * ocfs2_setattr() *must* take cluster locks to
  875. * proceeed, this will lead us to recursively lock the
  876. * inode. There's also the dinode i_size state which
  877. * can be lost via setattr during extending writes (we
  878. * set inode->i_size at the end of a write. */
  879. if (ocfs2_write_should_remove_suid(inode)) {
  880. if (meta_level == 0) {
  881. ocfs2_meta_unlock(inode, meta_level);
  882. meta_level = 1;
  883. continue;
  884. }
  885. ret = ocfs2_write_remove_suid(inode);
  886. if (ret < 0) {
  887. mlog_errno(ret);
  888. goto out;
  889. }
  890. }
  891. /* work on a copy of ppos until we're sure that we won't have
  892. * to recalculate it due to relocking. */
  893. if (filp->f_flags & O_APPEND) {
  894. saved_pos = i_size_read(inode);
  895. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  896. } else {
  897. saved_pos = iocb->ki_pos;
  898. }
  899. newsize = count + saved_pos;
  900. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  901. (long long) saved_pos, (long long) newsize,
  902. (long long) i_size_read(inode));
  903. /* No need for a higher level metadata lock if we're
  904. * never going past i_size. */
  905. if (newsize <= i_size_read(inode))
  906. break;
  907. if (meta_level == 0) {
  908. ocfs2_meta_unlock(inode, meta_level);
  909. meta_level = 1;
  910. continue;
  911. }
  912. spin_lock(&OCFS2_I(inode)->ip_lock);
  913. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  914. OCFS2_I(inode)->ip_clusters;
  915. spin_unlock(&OCFS2_I(inode)->ip_lock);
  916. mlog(0, "Writing at EOF, may need more allocation: "
  917. "i_size = %lld, newsize = %lld, need %u clusters\n",
  918. (long long) i_size_read(inode), (long long) newsize,
  919. clusters);
  920. /* We only want to continue the rest of this loop if
  921. * our extend will actually require more
  922. * allocation. */
  923. if (!clusters)
  924. break;
  925. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  926. if (ret < 0) {
  927. if (ret != -ENOSPC)
  928. mlog_errno(ret);
  929. goto out;
  930. }
  931. break;
  932. }
  933. /* ok, we're done with i_size and alloc work */
  934. iocb->ki_pos = saved_pos;
  935. ocfs2_meta_unlock(inode, meta_level);
  936. meta_level = -1;
  937. /* communicate with ocfs2_dio_end_io */
  938. ocfs2_iocb_set_rw_locked(iocb);
  939. ret = generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  940. /* buffered aio wouldn't have proper lock coverage today */
  941. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  942. /*
  943. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  944. * function pointer which is called when o_direct io completes so that
  945. * it can unlock our rw lock. (it's the clustered equivalent of
  946. * i_alloc_sem; protects truncate from racing with pending ios).
  947. * Unfortunately there are error cases which call end_io and others
  948. * that don't. so we don't have to unlock the rw_lock if either an
  949. * async dio is going to do it in the future or an end_io after an
  950. * error has already done it.
  951. */
  952. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  953. rw_level = -1;
  954. have_alloc_sem = 0;
  955. }
  956. out:
  957. if (meta_level != -1)
  958. ocfs2_meta_unlock(inode, meta_level);
  959. if (have_alloc_sem)
  960. up_read(&inode->i_alloc_sem);
  961. if (rw_level != -1)
  962. ocfs2_rw_unlock(inode, rw_level);
  963. mutex_unlock(&inode->i_mutex);
  964. mlog_exit(ret);
  965. return ret;
  966. }
  967. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  968. char __user *buf,
  969. size_t count,
  970. loff_t pos)
  971. {
  972. int ret = 0, rw_level = -1, have_alloc_sem = 0;
  973. struct file *filp = iocb->ki_filp;
  974. struct inode *inode = filp->f_dentry->d_inode;
  975. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
  976. (unsigned int)count,
  977. filp->f_dentry->d_name.len,
  978. filp->f_dentry->d_name.name);
  979. if (!inode) {
  980. ret = -EINVAL;
  981. mlog_errno(ret);
  982. goto bail;
  983. }
  984. /*
  985. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  986. * need locks to protect pending reads from racing with truncate.
  987. */
  988. if (filp->f_flags & O_DIRECT) {
  989. down_read(&inode->i_alloc_sem);
  990. have_alloc_sem = 1;
  991. ret = ocfs2_rw_lock(inode, 0);
  992. if (ret < 0) {
  993. mlog_errno(ret);
  994. goto bail;
  995. }
  996. rw_level = 0;
  997. /* communicate with ocfs2_dio_end_io */
  998. ocfs2_iocb_set_rw_locked(iocb);
  999. }
  1000. /*
  1001. * We're fine letting folks race truncates and extending
  1002. * writes with read across the cluster, just like they can
  1003. * locally. Hence no rw_lock during read.
  1004. *
  1005. * Take and drop the meta data lock to update inode fields
  1006. * like i_size. This allows the checks down below
  1007. * generic_file_aio_read() a chance of actually working.
  1008. */
  1009. ret = ocfs2_meta_lock(inode, NULL, NULL, 0);
  1010. if (ret < 0) {
  1011. mlog_errno(ret);
  1012. goto bail;
  1013. }
  1014. ocfs2_meta_unlock(inode, 0);
  1015. ret = generic_file_aio_read(iocb, buf, count, iocb->ki_pos);
  1016. if (ret == -EINVAL)
  1017. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1018. /* buffered aio wouldn't have proper lock coverage today */
  1019. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1020. /* see ocfs2_file_aio_write */
  1021. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1022. rw_level = -1;
  1023. have_alloc_sem = 0;
  1024. }
  1025. bail:
  1026. if (have_alloc_sem)
  1027. up_read(&inode->i_alloc_sem);
  1028. if (rw_level != -1)
  1029. ocfs2_rw_unlock(inode, rw_level);
  1030. mlog_exit(ret);
  1031. return ret;
  1032. }
  1033. struct inode_operations ocfs2_file_iops = {
  1034. .setattr = ocfs2_setattr,
  1035. .getattr = ocfs2_getattr,
  1036. };
  1037. struct inode_operations ocfs2_special_file_iops = {
  1038. .setattr = ocfs2_setattr,
  1039. .getattr = ocfs2_getattr,
  1040. };
  1041. const struct file_operations ocfs2_fops = {
  1042. .read = do_sync_read,
  1043. .write = do_sync_write,
  1044. .sendfile = generic_file_sendfile,
  1045. .mmap = ocfs2_mmap,
  1046. .fsync = ocfs2_sync_file,
  1047. .release = ocfs2_file_release,
  1048. .open = ocfs2_file_open,
  1049. .aio_read = ocfs2_file_aio_read,
  1050. .aio_write = ocfs2_file_aio_write,
  1051. };
  1052. const struct file_operations ocfs2_dops = {
  1053. .read = generic_read_dir,
  1054. .readdir = ocfs2_readdir,
  1055. .fsync = ocfs2_sync_file,
  1056. };