file.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 char __user *buf,
  823. size_t count,
  824. loff_t pos)
  825. {
  826. struct iovec local_iov = { .iov_base = (void __user *)buf,
  827. .iov_len = count };
  828. int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
  829. u32 clusters;
  830. struct file *filp = iocb->ki_filp;
  831. struct inode *inode = filp->f_dentry->d_inode;
  832. loff_t newsize, saved_pos;
  833. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
  834. (unsigned int)count,
  835. filp->f_dentry->d_name.len,
  836. filp->f_dentry->d_name.name);
  837. /* happy write of zero bytes */
  838. if (count == 0)
  839. return 0;
  840. if (!inode) {
  841. mlog(0, "bad inode\n");
  842. return -EIO;
  843. }
  844. mutex_lock(&inode->i_mutex);
  845. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  846. if (filp->f_flags & O_DIRECT) {
  847. have_alloc_sem = 1;
  848. down_read(&inode->i_alloc_sem);
  849. }
  850. /* concurrent O_DIRECT writes are allowed */
  851. rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
  852. ret = ocfs2_rw_lock(inode, rw_level);
  853. if (ret < 0) {
  854. rw_level = -1;
  855. mlog_errno(ret);
  856. goto out;
  857. }
  858. /*
  859. * We sample i_size under a read level meta lock to see if our write
  860. * is extending the file, if it is we back off and get a write level
  861. * meta lock.
  862. */
  863. meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
  864. for(;;) {
  865. ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level);
  866. if (ret < 0) {
  867. meta_level = -1;
  868. mlog_errno(ret);
  869. goto out;
  870. }
  871. /* Clear suid / sgid if necessary. We do this here
  872. * instead of later in the write path because
  873. * remove_suid() calls ->setattr without any hint that
  874. * we may have already done our cluster locking. Since
  875. * ocfs2_setattr() *must* take cluster locks to
  876. * proceeed, this will lead us to recursively lock the
  877. * inode. There's also the dinode i_size state which
  878. * can be lost via setattr during extending writes (we
  879. * set inode->i_size at the end of a write. */
  880. if (ocfs2_write_should_remove_suid(inode)) {
  881. if (meta_level == 0) {
  882. ocfs2_meta_unlock(inode, meta_level);
  883. meta_level = 1;
  884. continue;
  885. }
  886. ret = ocfs2_write_remove_suid(inode);
  887. if (ret < 0) {
  888. mlog_errno(ret);
  889. goto out;
  890. }
  891. }
  892. /* work on a copy of ppos until we're sure that we won't have
  893. * to recalculate it due to relocking. */
  894. if (filp->f_flags & O_APPEND) {
  895. saved_pos = i_size_read(inode);
  896. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  897. } else {
  898. saved_pos = iocb->ki_pos;
  899. }
  900. newsize = count + saved_pos;
  901. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  902. (long long) saved_pos, (long long) newsize,
  903. (long long) i_size_read(inode));
  904. /* No need for a higher level metadata lock if we're
  905. * never going past i_size. */
  906. if (newsize <= i_size_read(inode))
  907. break;
  908. if (meta_level == 0) {
  909. ocfs2_meta_unlock(inode, meta_level);
  910. meta_level = 1;
  911. continue;
  912. }
  913. spin_lock(&OCFS2_I(inode)->ip_lock);
  914. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  915. OCFS2_I(inode)->ip_clusters;
  916. spin_unlock(&OCFS2_I(inode)->ip_lock);
  917. mlog(0, "Writing at EOF, may need more allocation: "
  918. "i_size = %lld, newsize = %lld, need %u clusters\n",
  919. (long long) i_size_read(inode), (long long) newsize,
  920. clusters);
  921. /* We only want to continue the rest of this loop if
  922. * our extend will actually require more
  923. * allocation. */
  924. if (!clusters)
  925. break;
  926. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  927. if (ret < 0) {
  928. if (ret != -ENOSPC)
  929. mlog_errno(ret);
  930. goto out;
  931. }
  932. break;
  933. }
  934. /* ok, we're done with i_size and alloc work */
  935. iocb->ki_pos = saved_pos;
  936. ocfs2_meta_unlock(inode, meta_level);
  937. meta_level = -1;
  938. /* communicate with ocfs2_dio_end_io */
  939. ocfs2_iocb_set_rw_locked(iocb);
  940. ret = generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
  941. /* buffered aio wouldn't have proper lock coverage today */
  942. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  943. /*
  944. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  945. * function pointer which is called when o_direct io completes so that
  946. * it can unlock our rw lock. (it's the clustered equivalent of
  947. * i_alloc_sem; protects truncate from racing with pending ios).
  948. * Unfortunately there are error cases which call end_io and others
  949. * that don't. so we don't have to unlock the rw_lock if either an
  950. * async dio is going to do it in the future or an end_io after an
  951. * error has already done it.
  952. */
  953. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  954. rw_level = -1;
  955. have_alloc_sem = 0;
  956. }
  957. out:
  958. if (meta_level != -1)
  959. ocfs2_meta_unlock(inode, meta_level);
  960. if (have_alloc_sem)
  961. up_read(&inode->i_alloc_sem);
  962. if (rw_level != -1)
  963. ocfs2_rw_unlock(inode, rw_level);
  964. mutex_unlock(&inode->i_mutex);
  965. mlog_exit(ret);
  966. return ret;
  967. }
  968. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  969. char __user *buf,
  970. size_t count,
  971. loff_t pos)
  972. {
  973. int ret = 0, rw_level = -1, have_alloc_sem = 0;
  974. struct file *filp = iocb->ki_filp;
  975. struct inode *inode = filp->f_dentry->d_inode;
  976. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf,
  977. (unsigned int)count,
  978. filp->f_dentry->d_name.len,
  979. filp->f_dentry->d_name.name);
  980. if (!inode) {
  981. ret = -EINVAL;
  982. mlog_errno(ret);
  983. goto bail;
  984. }
  985. /*
  986. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  987. * need locks to protect pending reads from racing with truncate.
  988. */
  989. if (filp->f_flags & O_DIRECT) {
  990. down_read(&inode->i_alloc_sem);
  991. have_alloc_sem = 1;
  992. ret = ocfs2_rw_lock(inode, 0);
  993. if (ret < 0) {
  994. mlog_errno(ret);
  995. goto bail;
  996. }
  997. rw_level = 0;
  998. /* communicate with ocfs2_dio_end_io */
  999. ocfs2_iocb_set_rw_locked(iocb);
  1000. }
  1001. /*
  1002. * We're fine letting folks race truncates and extending
  1003. * writes with read across the cluster, just like they can
  1004. * locally. Hence no rw_lock during read.
  1005. *
  1006. * Take and drop the meta data lock to update inode fields
  1007. * like i_size. This allows the checks down below
  1008. * generic_file_aio_read() a chance of actually working.
  1009. */
  1010. ret = ocfs2_meta_lock(inode, NULL, NULL, 0);
  1011. if (ret < 0) {
  1012. mlog_errno(ret);
  1013. goto bail;
  1014. }
  1015. ocfs2_meta_unlock(inode, 0);
  1016. ret = generic_file_aio_read(iocb, buf, count, iocb->ki_pos);
  1017. if (ret == -EINVAL)
  1018. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1019. /* buffered aio wouldn't have proper lock coverage today */
  1020. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1021. /* see ocfs2_file_aio_write */
  1022. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1023. rw_level = -1;
  1024. have_alloc_sem = 0;
  1025. }
  1026. bail:
  1027. if (have_alloc_sem)
  1028. up_read(&inode->i_alloc_sem);
  1029. if (rw_level != -1)
  1030. ocfs2_rw_unlock(inode, rw_level);
  1031. mlog_exit(ret);
  1032. return ret;
  1033. }
  1034. struct inode_operations ocfs2_file_iops = {
  1035. .setattr = ocfs2_setattr,
  1036. .getattr = ocfs2_getattr,
  1037. };
  1038. struct inode_operations ocfs2_special_file_iops = {
  1039. .setattr = ocfs2_setattr,
  1040. .getattr = ocfs2_getattr,
  1041. };
  1042. const struct file_operations ocfs2_fops = {
  1043. .read = do_sync_read,
  1044. .write = do_sync_write,
  1045. .sendfile = generic_file_sendfile,
  1046. .mmap = ocfs2_mmap,
  1047. .fsync = ocfs2_sync_file,
  1048. .release = ocfs2_file_release,
  1049. .open = ocfs2_file_open,
  1050. .aio_read = ocfs2_file_aio_read,
  1051. .aio_write = ocfs2_file_aio_write,
  1052. .ioctl = ocfs2_ioctl,
  1053. };
  1054. const struct file_operations ocfs2_dops = {
  1055. .read = generic_read_dir,
  1056. .readdir = ocfs2_readdir,
  1057. .fsync = ocfs2_sync_file,
  1058. .ioctl = ocfs2_ioctl,
  1059. };