file.c 30 KB

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