inode.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * inode.c
  5. *
  6. * vfs' aops, fops, dops and iops
  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/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/smp_lock.h>
  31. #include <asm/byteorder.h>
  32. #define MLOG_MASK_PREFIX ML_INODE
  33. #include <cluster/masklog.h>
  34. #include "ocfs2.h"
  35. #include "alloc.h"
  36. #include "dlmglue.h"
  37. #include "extent_map.h"
  38. #include "file.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "namei.h"
  42. #include "suballoc.h"
  43. #include "super.h"
  44. #include "symlink.h"
  45. #include "sysfile.h"
  46. #include "uptodate.h"
  47. #include "vote.h"
  48. #include "buffer_head_io.h"
  49. #define OCFS2_FI_FLAG_NOWAIT 0x1
  50. #define OCFS2_FI_FLAG_DELETE 0x2
  51. struct ocfs2_find_inode_args
  52. {
  53. u64 fi_blkno;
  54. unsigned long fi_ino;
  55. unsigned int fi_flags;
  56. };
  57. static int ocfs2_read_locked_inode(struct inode *inode,
  58. struct ocfs2_find_inode_args *args);
  59. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  60. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  61. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  62. struct inode *inode,
  63. struct buffer_head *fe_bh);
  64. struct inode *ocfs2_ilookup_for_vote(struct ocfs2_super *osb,
  65. u64 blkno,
  66. int delete_vote)
  67. {
  68. struct ocfs2_find_inode_args args;
  69. /* ocfs2_ilookup_for_vote should *only* be called from the
  70. * vote thread */
  71. BUG_ON(current != osb->vote_task);
  72. args.fi_blkno = blkno;
  73. args.fi_flags = OCFS2_FI_FLAG_NOWAIT;
  74. if (delete_vote)
  75. args.fi_flags |= OCFS2_FI_FLAG_DELETE;
  76. args.fi_ino = ino_from_blkno(osb->sb, blkno);
  77. return ilookup5(osb->sb, args.fi_ino, ocfs2_find_actor, &args);
  78. }
  79. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno)
  80. {
  81. struct inode *inode = NULL;
  82. struct super_block *sb = osb->sb;
  83. struct ocfs2_find_inode_args args;
  84. mlog_entry("(blkno = %"MLFu64")\n", blkno);
  85. /* Ok. By now we've either got the offsets passed to us by the
  86. * caller, or we just pulled them off the bh. Lets do some
  87. * sanity checks to make sure they're OK. */
  88. if (blkno == 0) {
  89. inode = ERR_PTR(-EINVAL);
  90. mlog_errno(PTR_ERR(inode));
  91. goto bail;
  92. }
  93. args.fi_blkno = blkno;
  94. args.fi_flags = 0;
  95. args.fi_ino = ino_from_blkno(sb, blkno);
  96. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  97. ocfs2_init_locked_inode, &args);
  98. /* inode was *not* in the inode cache. 2.6.x requires
  99. * us to do our own read_inode call and unlock it
  100. * afterwards. */
  101. if (inode && inode->i_state & I_NEW) {
  102. mlog(0, "Inode was not in inode cache, reading it.\n");
  103. ocfs2_read_locked_inode(inode, &args);
  104. unlock_new_inode(inode);
  105. }
  106. if (inode == NULL) {
  107. inode = ERR_PTR(-ENOMEM);
  108. mlog_errno(PTR_ERR(inode));
  109. goto bail;
  110. }
  111. if (is_bad_inode(inode)) {
  112. iput(inode);
  113. inode = ERR_PTR(-ESTALE);
  114. mlog_errno(PTR_ERR(inode));
  115. goto bail;
  116. }
  117. bail:
  118. if (!IS_ERR(inode)) {
  119. mlog(0, "returning inode with number %"MLFu64"\n",
  120. OCFS2_I(inode)->ip_blkno);
  121. mlog_exit_ptr(inode);
  122. } else
  123. mlog_errno(PTR_ERR(inode));
  124. return inode;
  125. }
  126. /*
  127. * here's how inodes get read from disk:
  128. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  129. * found? : return the in-memory inode
  130. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  131. */
  132. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  133. {
  134. struct ocfs2_find_inode_args *args = NULL;
  135. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  136. int ret = 0;
  137. mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
  138. args = opaque;
  139. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  140. if (oi->ip_blkno != args->fi_blkno)
  141. goto bail;
  142. /* OCFS2_FI_FLAG_NOWAIT is *only* set from
  143. * ocfs2_ilookup_for_vote which won't create an inode for one
  144. * that isn't found. The vote thread which doesn't want to get
  145. * an inode which is in the process of going away - otherwise
  146. * the call to __wait_on_freeing_inode in find_inode_fast will
  147. * cause it to deadlock on an inode which may be waiting on a
  148. * vote (or lock release) in delete_inode */
  149. if ((args->fi_flags & OCFS2_FI_FLAG_NOWAIT) &&
  150. (inode->i_state & (I_FREEING|I_CLEAR))) {
  151. /* As stated above, we're not going to return an
  152. * inode. In the case of a delete vote, the voting
  153. * code is going to signal the other node to go
  154. * ahead. Mark that state here, so this freeing inode
  155. * has the state when it gets to delete_inode. */
  156. if (args->fi_flags & OCFS2_FI_FLAG_DELETE) {
  157. spin_lock(&oi->ip_lock);
  158. ocfs2_mark_inode_remotely_deleted(inode);
  159. spin_unlock(&oi->ip_lock);
  160. }
  161. goto bail;
  162. }
  163. ret = 1;
  164. bail:
  165. mlog_exit(ret);
  166. return ret;
  167. }
  168. /*
  169. * initialize the new inode, but don't do anything that would cause
  170. * us to sleep.
  171. * return 0 on success, 1 on failure
  172. */
  173. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  174. {
  175. struct ocfs2_find_inode_args *args = opaque;
  176. mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
  177. inode->i_ino = args->fi_ino;
  178. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  179. mlog_exit(0);
  180. return 0;
  181. }
  182. int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  183. int create_ino)
  184. {
  185. struct super_block *sb;
  186. struct ocfs2_super *osb;
  187. int status = -EINVAL;
  188. mlog_entry("(0x%p, size:%"MLFu64")\n", inode, fe->i_size);
  189. sb = inode->i_sb;
  190. osb = OCFS2_SB(sb);
  191. /* this means that read_inode cannot create a superblock inode
  192. * today. change if needed. */
  193. if (!OCFS2_IS_VALID_DINODE(fe) ||
  194. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  195. mlog(ML_ERROR, "Invalid dinode: i_ino=%lu, i_blkno=%"MLFu64", "
  196. "signature = %.*s, flags = 0x%x\n",
  197. inode->i_ino, le64_to_cpu(fe->i_blkno), 7,
  198. fe->i_signature, le32_to_cpu(fe->i_flags));
  199. goto bail;
  200. }
  201. if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
  202. mlog(ML_ERROR, "file entry generation does not match "
  203. "superblock! osb->fs_generation=%x, "
  204. "fe->i_fs_generation=%x\n",
  205. osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
  206. goto bail;
  207. }
  208. inode->i_version = 1;
  209. inode->i_generation = le32_to_cpu(fe->i_generation);
  210. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  211. inode->i_mode = le16_to_cpu(fe->i_mode);
  212. inode->i_uid = le32_to_cpu(fe->i_uid);
  213. inode->i_gid = le32_to_cpu(fe->i_gid);
  214. inode->i_blksize = (u32)osb->s_clustersize;
  215. /* Fast symlinks will have i_size but no allocated clusters. */
  216. if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
  217. inode->i_blocks = 0;
  218. else
  219. inode->i_blocks =
  220. ocfs2_align_bytes_to_sectors(le64_to_cpu(fe->i_size));
  221. inode->i_mapping->a_ops = &ocfs2_aops;
  222. inode->i_flags |= S_NOATIME;
  223. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  224. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  225. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  226. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  227. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  228. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  229. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  230. mlog(ML_ERROR,
  231. "ip_blkno %"MLFu64" != i_blkno %"MLFu64"!\n",
  232. OCFS2_I(inode)->ip_blkno, fe->i_blkno);
  233. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  234. OCFS2_I(inode)->ip_orphaned_slot = OCFS2_INVALID_SLOT;
  235. if (create_ino)
  236. inode->i_ino = ino_from_blkno(inode->i_sb,
  237. le64_to_cpu(fe->i_blkno));
  238. mlog(0, "blkno = %"MLFu64", ino = %lu, create_ino = %s\n",
  239. fe->i_blkno, inode->i_ino, create_ino ? "true" : "false");
  240. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  241. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  242. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  243. mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
  244. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  245. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  246. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  247. mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
  248. /* we can't actually hit this as read_inode can't
  249. * handle superblocks today ;-) */
  250. BUG();
  251. }
  252. switch (inode->i_mode & S_IFMT) {
  253. case S_IFREG:
  254. inode->i_fop = &ocfs2_fops;
  255. inode->i_op = &ocfs2_file_iops;
  256. i_size_write(inode, le64_to_cpu(fe->i_size));
  257. break;
  258. case S_IFDIR:
  259. inode->i_op = &ocfs2_dir_iops;
  260. inode->i_fop = &ocfs2_dops;
  261. i_size_write(inode, le64_to_cpu(fe->i_size));
  262. break;
  263. case S_IFLNK:
  264. if (ocfs2_inode_is_fast_symlink(inode))
  265. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  266. else
  267. inode->i_op = &ocfs2_symlink_inode_operations;
  268. i_size_write(inode, le64_to_cpu(fe->i_size));
  269. break;
  270. default:
  271. inode->i_op = &ocfs2_special_file_iops;
  272. init_special_inode(inode, inode->i_mode,
  273. inode->i_rdev);
  274. break;
  275. }
  276. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  277. OCFS2_LOCK_TYPE_RW, inode);
  278. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres,
  279. OCFS2_LOCK_TYPE_META, inode);
  280. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_data_lockres,
  281. OCFS2_LOCK_TYPE_DATA, inode);
  282. status = 0;
  283. bail:
  284. mlog_exit(status);
  285. return status;
  286. }
  287. static int ocfs2_read_locked_inode(struct inode *inode,
  288. struct ocfs2_find_inode_args *args)
  289. {
  290. struct super_block *sb;
  291. struct ocfs2_super *osb;
  292. struct ocfs2_dinode *fe;
  293. struct buffer_head *bh = NULL;
  294. int status;
  295. int sysfile = 0;
  296. mlog_entry("(0x%p, 0x%p)\n", inode, args);
  297. status = -EINVAL;
  298. if (inode == NULL || inode->i_sb == NULL) {
  299. mlog(ML_ERROR, "bad inode\n");
  300. goto bail;
  301. }
  302. sb = inode->i_sb;
  303. osb = OCFS2_SB(sb);
  304. if (!args) {
  305. mlog(ML_ERROR, "bad inode args\n");
  306. make_bad_inode(inode);
  307. goto bail;
  308. }
  309. /* Read the FE off disk. This is safe because the kernel only
  310. * does one read_inode2 for a new inode, and if it doesn't
  311. * exist yet then nobody can be working on it! */
  312. status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0, NULL);
  313. if (status < 0) {
  314. mlog_errno(status);
  315. make_bad_inode(inode);
  316. goto bail;
  317. }
  318. fe = (struct ocfs2_dinode *) bh->b_data;
  319. if (!OCFS2_IS_VALID_DINODE(fe)) {
  320. mlog(ML_ERROR, "Invalid dinode #%"MLFu64": signature = %.*s\n",
  321. fe->i_blkno, 7, fe->i_signature);
  322. make_bad_inode(inode);
  323. goto bail;
  324. }
  325. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
  326. sysfile = 1;
  327. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  328. S_ISBLK(le16_to_cpu(fe->i_mode)))
  329. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  330. status = -EINVAL;
  331. if (ocfs2_populate_inode(inode, fe, 0) < 0) {
  332. mlog(ML_ERROR, "populate inode failed! i_blkno=%"MLFu64", "
  333. "i_ino=%lu\n", fe->i_blkno, inode->i_ino);
  334. make_bad_inode(inode);
  335. goto bail;
  336. }
  337. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  338. if (sysfile)
  339. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  340. status = 0;
  341. bail:
  342. if (args && bh)
  343. brelse(bh);
  344. mlog_exit(status);
  345. return status;
  346. }
  347. void ocfs2_sync_blockdev(struct super_block *sb)
  348. {
  349. sync_blockdev(sb->s_bdev);
  350. }
  351. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  352. struct inode *inode,
  353. struct buffer_head *fe_bh)
  354. {
  355. int status = 0;
  356. struct ocfs2_journal_handle *handle = NULL;
  357. struct ocfs2_truncate_context *tc = NULL;
  358. struct ocfs2_dinode *fe;
  359. mlog_entry_void();
  360. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  361. /* zero allocation, zero truncate :) */
  362. if (!fe->i_clusters)
  363. goto bail;
  364. handle = ocfs2_start_trans(osb, handle, OCFS2_INODE_UPDATE_CREDITS);
  365. if (IS_ERR(handle)) {
  366. status = PTR_ERR(handle);
  367. handle = NULL;
  368. mlog_errno(status);
  369. goto bail;
  370. }
  371. status = ocfs2_set_inode_size(handle, inode, fe_bh, 0ULL);
  372. if (status < 0) {
  373. mlog_errno(status);
  374. goto bail;
  375. }
  376. ocfs2_commit_trans(handle);
  377. handle = NULL;
  378. status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
  379. if (status < 0) {
  380. mlog_errno(status);
  381. goto bail;
  382. }
  383. status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
  384. if (status < 0) {
  385. mlog_errno(status);
  386. goto bail;
  387. }
  388. bail:
  389. if (handle)
  390. ocfs2_commit_trans(handle);
  391. mlog_exit(status);
  392. return status;
  393. }
  394. static int ocfs2_remove_inode(struct inode *inode,
  395. struct buffer_head *di_bh,
  396. struct inode *orphan_dir_inode,
  397. struct buffer_head *orphan_dir_bh)
  398. {
  399. int status;
  400. struct inode *inode_alloc_inode = NULL;
  401. struct buffer_head *inode_alloc_bh = NULL;
  402. struct ocfs2_journal_handle *handle;
  403. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  404. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  405. inode_alloc_inode =
  406. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  407. le16_to_cpu(di->i_suballoc_slot));
  408. if (!inode_alloc_inode) {
  409. status = -EEXIST;
  410. mlog_errno(status);
  411. goto bail;
  412. }
  413. mutex_lock(&inode_alloc_inode->i_mutex);
  414. status = ocfs2_meta_lock(inode_alloc_inode, NULL, &inode_alloc_bh, 1);
  415. if (status < 0) {
  416. mutex_unlock(&inode_alloc_inode->i_mutex);
  417. mlog_errno(status);
  418. goto bail;
  419. }
  420. handle = ocfs2_start_trans(osb, NULL, OCFS2_DELETE_INODE_CREDITS);
  421. if (IS_ERR(handle)) {
  422. status = PTR_ERR(handle);
  423. mlog_errno(status);
  424. goto bail_unlock;
  425. }
  426. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  427. orphan_dir_bh);
  428. if (status < 0) {
  429. mlog_errno(status);
  430. goto bail_commit;
  431. }
  432. /* set the inodes dtime */
  433. status = ocfs2_journal_access(handle, inode, di_bh,
  434. OCFS2_JOURNAL_ACCESS_WRITE);
  435. if (status < 0) {
  436. mlog_errno(status);
  437. goto bail_commit;
  438. }
  439. di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
  440. le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  441. status = ocfs2_journal_dirty(handle, di_bh);
  442. if (status < 0) {
  443. mlog_errno(status);
  444. goto bail_commit;
  445. }
  446. ocfs2_remove_from_cache(inode, di_bh);
  447. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  448. inode_alloc_bh, di);
  449. if (status < 0)
  450. mlog_errno(status);
  451. bail_commit:
  452. ocfs2_commit_trans(handle);
  453. bail_unlock:
  454. ocfs2_meta_unlock(inode_alloc_inode, 1);
  455. mutex_unlock(&inode_alloc_inode->i_mutex);
  456. brelse(inode_alloc_bh);
  457. bail:
  458. iput(inode_alloc_inode);
  459. return status;
  460. }
  461. static int ocfs2_wipe_inode(struct inode *inode,
  462. struct buffer_head *di_bh)
  463. {
  464. int status, orphaned_slot;
  465. struct inode *orphan_dir_inode = NULL;
  466. struct buffer_head *orphan_dir_bh = NULL;
  467. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  468. /* We've already voted on this so it should be readonly - no
  469. * spinlock needed. */
  470. orphaned_slot = OCFS2_I(inode)->ip_orphaned_slot;
  471. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  472. ORPHAN_DIR_SYSTEM_INODE,
  473. orphaned_slot);
  474. if (!orphan_dir_inode) {
  475. status = -EEXIST;
  476. mlog_errno(status);
  477. goto bail;
  478. }
  479. /* Lock the orphan dir. The lock will be held for the entire
  480. * delete_inode operation. We do this now to avoid races with
  481. * recovery completion on other nodes. */
  482. mutex_lock(&orphan_dir_inode->i_mutex);
  483. status = ocfs2_meta_lock(orphan_dir_inode, NULL, &orphan_dir_bh, 1);
  484. if (status < 0) {
  485. mutex_unlock(&orphan_dir_inode->i_mutex);
  486. mlog_errno(status);
  487. goto bail;
  488. }
  489. /* we do this while holding the orphan dir lock because we
  490. * don't want recovery being run from another node to vote for
  491. * an inode delete on us -- this will result in two nodes
  492. * truncating the same file! */
  493. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  494. if (status < 0) {
  495. mlog_errno(status);
  496. goto bail_unlock_dir;
  497. }
  498. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  499. orphan_dir_bh);
  500. if (status < 0)
  501. mlog_errno(status);
  502. bail_unlock_dir:
  503. ocfs2_meta_unlock(orphan_dir_inode, 1);
  504. mutex_unlock(&orphan_dir_inode->i_mutex);
  505. brelse(orphan_dir_bh);
  506. bail:
  507. iput(orphan_dir_inode);
  508. return status;
  509. }
  510. /* There is a series of simple checks that should be done before a
  511. * vote is even considered. Encapsulate those in this function. */
  512. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  513. {
  514. int ret = 0;
  515. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  516. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  517. /* We shouldn't be getting here for the root directory
  518. * inode.. */
  519. if (inode == osb->root_inode) {
  520. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  521. goto bail;
  522. }
  523. /* If we're coming from process_vote we can't go into our own
  524. * voting [hello, deadlock city!], so unforuntately we just
  525. * have to skip deleting this guy. That's OK though because
  526. * the node who's doing the actual deleting should handle it
  527. * anyway. */
  528. if (current == osb->vote_task) {
  529. mlog(0, "Skipping delete of %lu because we're currently "
  530. "in process_vote\n", inode->i_ino);
  531. goto bail;
  532. }
  533. spin_lock(&oi->ip_lock);
  534. /* OCFS2 *never* deletes system files. This should technically
  535. * never get here as system file inodes should always have a
  536. * positive link count. */
  537. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  538. mlog(ML_ERROR, "Skipping delete of system file %"MLFu64".\n",
  539. oi->ip_blkno);
  540. goto bail_unlock;
  541. }
  542. /* If we have voted "yes" on the wipe of this inode for
  543. * another node, it will be marked here so we can safely skip
  544. * it. Recovery will cleanup any inodes we might inadvertantly
  545. * skip here. */
  546. if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
  547. mlog(0, "Skipping delete of %lu because another node "
  548. "has done this for us.\n", inode->i_ino);
  549. goto bail_unlock;
  550. }
  551. ret = 1;
  552. bail_unlock:
  553. spin_unlock(&oi->ip_lock);
  554. bail:
  555. return ret;
  556. }
  557. /* Query the cluster to determine whether we should wipe an inode from
  558. * disk or not.
  559. *
  560. * Requires the inode to have the cluster lock. */
  561. static int ocfs2_query_inode_wipe(struct inode *inode,
  562. struct buffer_head *di_bh,
  563. int *wipe)
  564. {
  565. int status = 0;
  566. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  567. struct ocfs2_dinode *di;
  568. *wipe = 0;
  569. /* While we were waiting for the cluster lock in
  570. * ocfs2_delete_inode, another node might have asked to delete
  571. * the inode. Recheck our flags to catch this. */
  572. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  573. mlog(0, "Skipping delete of %"MLFu64" because flags changed\n",
  574. oi->ip_blkno);
  575. goto bail;
  576. }
  577. /* Now that we have an up to date inode, we can double check
  578. * the link count. */
  579. if (inode->i_nlink) {
  580. mlog(0, "Skipping delete of %"MLFu64" because nlink = %u\n",
  581. oi->ip_blkno, inode->i_nlink);
  582. goto bail;
  583. }
  584. /* Do some basic inode verification... */
  585. di = (struct ocfs2_dinode *) di_bh->b_data;
  586. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
  587. /* for lack of a better error? */
  588. status = -EEXIST;
  589. mlog(ML_ERROR,
  590. "Inode %"MLFu64" (on-disk %"MLFu64") not orphaned! "
  591. "Disk flags 0x%x, inode flags 0x%x\n",
  592. oi->ip_blkno, di->i_blkno, di->i_flags, oi->ip_flags);
  593. goto bail;
  594. }
  595. /* has someone already deleted us?! baaad... */
  596. if (di->i_dtime) {
  597. status = -EEXIST;
  598. mlog_errno(status);
  599. goto bail;
  600. }
  601. status = ocfs2_request_delete_vote(inode);
  602. /* -EBUSY means that other nodes are still using the
  603. * inode. We're done here though, so avoid doing anything on
  604. * disk and let them worry about deleting it. */
  605. if (status == -EBUSY) {
  606. status = 0;
  607. mlog(0, "Skipping delete of %"MLFu64" because it is in use on"
  608. "other nodes\n", oi->ip_blkno);
  609. goto bail;
  610. }
  611. if (status < 0) {
  612. mlog_errno(status);
  613. goto bail;
  614. }
  615. spin_lock(&oi->ip_lock);
  616. if (oi->ip_orphaned_slot == OCFS2_INVALID_SLOT) {
  617. /* Nobody knew which slot this inode was orphaned
  618. * into. This may happen during node death and
  619. * recovery knows how to clean it up so we can safely
  620. * ignore this inode for now on. */
  621. mlog(0, "Nobody knew where inode %"MLFu64" was orphaned!\n",
  622. oi->ip_blkno);
  623. } else {
  624. *wipe = 1;
  625. mlog(0, "Inode %"MLFu64" is ok to wipe from orphan dir %d\n",
  626. oi->ip_blkno, oi->ip_orphaned_slot);
  627. }
  628. spin_unlock(&oi->ip_lock);
  629. bail:
  630. return status;
  631. }
  632. /* Support function for ocfs2_delete_inode. Will help us keep the
  633. * inode data in a consistent state for clear_inode. Always truncates
  634. * pages, optionally sync's them first. */
  635. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  636. int sync_data)
  637. {
  638. mlog(0, "Cleanup inode %"MLFu64", sync = %d\n",
  639. OCFS2_I(inode)->ip_blkno, sync_data);
  640. if (sync_data)
  641. write_inode_now(inode, 1);
  642. truncate_inode_pages(&inode->i_data, 0);
  643. }
  644. void ocfs2_delete_inode(struct inode *inode)
  645. {
  646. int wipe, status;
  647. sigset_t blocked, oldset;
  648. struct buffer_head *di_bh = NULL;
  649. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  650. if (is_bad_inode(inode)) {
  651. mlog(0, "Skipping delete of bad inode\n");
  652. goto bail;
  653. }
  654. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  655. /* It's probably not necessary to truncate_inode_pages
  656. * here but we do it for safety anyway (it will most
  657. * likely be a no-op anyway) */
  658. ocfs2_cleanup_delete_inode(inode, 0);
  659. goto bail;
  660. }
  661. /* We want to block signals in delete_inode as the lock and
  662. * messaging paths may return us -ERESTARTSYS. Which would
  663. * cause us to exit early, resulting in inodes being orphaned
  664. * forever. */
  665. sigfillset(&blocked);
  666. status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
  667. if (status < 0) {
  668. mlog_errno(status);
  669. ocfs2_cleanup_delete_inode(inode, 1);
  670. goto bail;
  671. }
  672. /* Lock down the inode. This gives us an up to date view of
  673. * it's metadata (for verification), and allows us to
  674. * serialize delete_inode votes.
  675. *
  676. * Even though we might be doing a truncate, we don't take the
  677. * allocation lock here as it won't be needed - nobody will
  678. * have the file open.
  679. */
  680. status = ocfs2_meta_lock(inode, NULL, &di_bh, 1);
  681. if (status < 0) {
  682. if (status != -ENOENT)
  683. mlog_errno(status);
  684. ocfs2_cleanup_delete_inode(inode, 0);
  685. goto bail_unblock;
  686. }
  687. /* Query the cluster. This will be the final decision made
  688. * before we go ahead and wipe the inode. */
  689. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  690. if (!wipe || status < 0) {
  691. /* Error and inode busy vote both mean we won't be
  692. * removing the inode, so they take almost the same
  693. * path. */
  694. if (status < 0)
  695. mlog_errno(status);
  696. /* Someone in the cluster has voted to not wipe this
  697. * inode, or it was never completely orphaned. Write
  698. * out the pages and exit now. */
  699. ocfs2_cleanup_delete_inode(inode, 1);
  700. goto bail_unlock_inode;
  701. }
  702. ocfs2_cleanup_delete_inode(inode, 0);
  703. status = ocfs2_wipe_inode(inode, di_bh);
  704. if (status < 0) {
  705. mlog_errno(status);
  706. goto bail_unlock_inode;
  707. }
  708. /* Mark the inode as successfully deleted. This is important
  709. * for ocfs2_clear_inode as it will check this flag and skip
  710. * any checkpointing work */
  711. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  712. bail_unlock_inode:
  713. ocfs2_meta_unlock(inode, 1);
  714. brelse(di_bh);
  715. bail_unblock:
  716. status = sigprocmask(SIG_SETMASK, &oldset, NULL);
  717. if (status < 0)
  718. mlog_errno(status);
  719. bail:
  720. clear_inode(inode);
  721. mlog_exit_void();
  722. }
  723. void ocfs2_clear_inode(struct inode *inode)
  724. {
  725. int status;
  726. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  727. mlog_entry_void();
  728. if (!inode)
  729. goto bail;
  730. mlog(0, "Clearing inode: %"MLFu64", nlink = %u\n",
  731. OCFS2_I(inode)->ip_blkno, inode->i_nlink);
  732. mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  733. "Inode=%lu\n", inode->i_ino);
  734. /* Do these before all the other work so that we don't bounce
  735. * the vote thread while waiting to destroy the locks. */
  736. ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
  737. ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres);
  738. ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
  739. /* We very well may get a clear_inode before all an inodes
  740. * metadata has hit disk. Of course, we can't drop any cluster
  741. * locks until the journal has finished with it. The only
  742. * exception here are successfully wiped inodes - their
  743. * metadata can now be considered to be part of the system
  744. * inodes from which it came. */
  745. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  746. ocfs2_checkpoint_inode(inode);
  747. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  748. "Clear inode of %"MLFu64", inode has io markers\n",
  749. oi->ip_blkno);
  750. ocfs2_extent_map_drop(inode, 0);
  751. ocfs2_extent_map_init(inode);
  752. status = ocfs2_drop_inode_locks(inode);
  753. if (status < 0)
  754. mlog_errno(status);
  755. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  756. ocfs2_lock_res_free(&oi->ip_meta_lockres);
  757. ocfs2_lock_res_free(&oi->ip_data_lockres);
  758. ocfs2_metadata_cache_purge(inode);
  759. mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
  760. "Clear inode of %"MLFu64", inode has %u cache items\n",
  761. oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
  762. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  763. "Clear inode of %"MLFu64", inode has a bad flag\n",
  764. oi->ip_blkno);
  765. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  766. "Clear inode of %"MLFu64", inode is locked\n",
  767. oi->ip_blkno);
  768. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  769. "Clear inode of %"MLFu64", io_mutex is locked\n",
  770. oi->ip_blkno);
  771. mutex_unlock(&oi->ip_io_mutex);
  772. /*
  773. * down_trylock() returns 0, down_write_trylock() returns 1
  774. * kernel 1, world 0
  775. */
  776. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  777. "Clear inode of %"MLFu64", alloc_sem is locked\n",
  778. oi->ip_blkno);
  779. up_write(&oi->ip_alloc_sem);
  780. mlog_bug_on_msg(oi->ip_open_count,
  781. "Clear inode of %"MLFu64" has open count %d\n",
  782. oi->ip_blkno, oi->ip_open_count);
  783. mlog_bug_on_msg(!list_empty(&oi->ip_handle_list),
  784. "Clear inode of %"MLFu64" has non empty handle list\n",
  785. oi->ip_blkno);
  786. mlog_bug_on_msg(oi->ip_handle,
  787. "Clear inode of %"MLFu64" has non empty handle pointer\n",
  788. oi->ip_blkno);
  789. /* Clear all other flags. */
  790. oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
  791. oi->ip_created_trans = 0;
  792. oi->ip_last_trans = 0;
  793. oi->ip_dir_start_lookup = 0;
  794. oi->ip_blkno = 0ULL;
  795. bail:
  796. mlog_exit_void();
  797. }
  798. /* Called under inode_lock, with no more references on the
  799. * struct inode, so it's safe here to check the flags field
  800. * and to manipulate i_nlink without any other locks. */
  801. void ocfs2_drop_inode(struct inode *inode)
  802. {
  803. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  804. mlog_entry_void();
  805. mlog(0, "Drop inode %"MLFu64", nlink = %u, ip_flags = 0x%x\n",
  806. oi->ip_blkno, inode->i_nlink, oi->ip_flags);
  807. /* Testing ip_orphaned_slot here wouldn't work because we may
  808. * not have gotten a delete_inode vote from any other nodes
  809. * yet. */
  810. if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED) {
  811. mlog(0, "Inode was orphaned on another node, clearing nlink.\n");
  812. inode->i_nlink = 0;
  813. }
  814. generic_drop_inode(inode);
  815. mlog_exit_void();
  816. }
  817. /*
  818. * TODO: this should probably be merged into ocfs2_get_block
  819. *
  820. * However, you now need to pay attention to the cont_prepare_write()
  821. * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
  822. * expects never to extend).
  823. */
  824. struct buffer_head *ocfs2_bread(struct inode *inode,
  825. int block, int *err, int reada)
  826. {
  827. struct buffer_head *bh = NULL;
  828. int tmperr;
  829. u64 p_blkno;
  830. int readflags = OCFS2_BH_CACHED;
  831. #if 0
  832. /* only turn this on if we know we can deal with read_block
  833. * returning nothing */
  834. if (reada)
  835. readflags |= OCFS2_BH_READAHEAD;
  836. #endif
  837. if (((u64)block << inode->i_sb->s_blocksize_bits) >=
  838. i_size_read(inode)) {
  839. BUG_ON(!reada);
  840. return NULL;
  841. }
  842. tmperr = ocfs2_extent_map_get_blocks(inode, block, 1,
  843. &p_blkno, NULL);
  844. if (tmperr < 0) {
  845. mlog_errno(tmperr);
  846. goto fail;
  847. }
  848. tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
  849. readflags, inode);
  850. if (tmperr < 0)
  851. goto fail;
  852. tmperr = 0;
  853. *err = 0;
  854. return bh;
  855. fail:
  856. if (bh) {
  857. brelse(bh);
  858. bh = NULL;
  859. }
  860. *err = -EIO;
  861. return NULL;
  862. }
  863. /*
  864. * This is called from our getattr.
  865. */
  866. int ocfs2_inode_revalidate(struct dentry *dentry)
  867. {
  868. struct inode *inode = dentry->d_inode;
  869. int status = 0;
  870. mlog_entry("(inode = 0x%p, ino = %"MLFu64")\n", inode,
  871. inode ? OCFS2_I(inode)->ip_blkno : 0ULL);
  872. if (!inode) {
  873. mlog(0, "eep, no inode!\n");
  874. status = -ENOENT;
  875. goto bail;
  876. }
  877. spin_lock(&OCFS2_I(inode)->ip_lock);
  878. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  879. spin_unlock(&OCFS2_I(inode)->ip_lock);
  880. mlog(0, "inode deleted!\n");
  881. status = -ENOENT;
  882. goto bail;
  883. }
  884. spin_unlock(&OCFS2_I(inode)->ip_lock);
  885. /* Let ocfs2_meta_lock do the work of updating our struct
  886. * inode for us. */
  887. status = ocfs2_meta_lock(inode, NULL, NULL, 0);
  888. if (status < 0) {
  889. if (status != -ENOENT)
  890. mlog_errno(status);
  891. goto bail;
  892. }
  893. ocfs2_meta_unlock(inode, 0);
  894. bail:
  895. mlog_exit(status);
  896. return status;
  897. }
  898. /*
  899. * Updates a disk inode from a
  900. * struct inode.
  901. * Only takes ip_lock.
  902. */
  903. int ocfs2_mark_inode_dirty(struct ocfs2_journal_handle *handle,
  904. struct inode *inode,
  905. struct buffer_head *bh)
  906. {
  907. int status;
  908. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  909. mlog_entry("(inode %"MLFu64")\n", OCFS2_I(inode)->ip_blkno);
  910. status = ocfs2_journal_access(handle, inode, bh,
  911. OCFS2_JOURNAL_ACCESS_WRITE);
  912. if (status < 0) {
  913. mlog_errno(status);
  914. goto leave;
  915. }
  916. spin_lock(&OCFS2_I(inode)->ip_lock);
  917. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  918. spin_unlock(&OCFS2_I(inode)->ip_lock);
  919. fe->i_size = cpu_to_le64(i_size_read(inode));
  920. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  921. fe->i_uid = cpu_to_le32(inode->i_uid);
  922. fe->i_gid = cpu_to_le32(inode->i_gid);
  923. fe->i_mode = cpu_to_le16(inode->i_mode);
  924. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  925. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  926. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  927. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  928. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  929. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  930. status = ocfs2_journal_dirty(handle, bh);
  931. if (status < 0)
  932. mlog_errno(status);
  933. status = 0;
  934. leave:
  935. mlog_exit(status);
  936. return status;
  937. }
  938. /*
  939. *
  940. * Updates a struct inode from a disk inode.
  941. * does no i/o, only takes ip_lock.
  942. */
  943. void ocfs2_refresh_inode(struct inode *inode,
  944. struct ocfs2_dinode *fe)
  945. {
  946. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  947. spin_lock(&OCFS2_I(inode)->ip_lock);
  948. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  949. i_size_write(inode, le64_to_cpu(fe->i_size));
  950. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  951. inode->i_uid = le32_to_cpu(fe->i_uid);
  952. inode->i_gid = le32_to_cpu(fe->i_gid);
  953. inode->i_mode = le16_to_cpu(fe->i_mode);
  954. inode->i_blksize = (u32) osb->s_clustersize;
  955. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  956. inode->i_blocks = 0;
  957. else
  958. inode->i_blocks = ocfs2_align_bytes_to_sectors(i_size_read(inode));
  959. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  960. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  961. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  962. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  963. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  964. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  965. spin_unlock(&OCFS2_I(inode)->ip_lock);
  966. }