inode.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 <asm/byteorder.h>
  31. #define MLOG_MASK_PREFIX ML_INODE
  32. #include <cluster/masklog.h>
  33. #include "ocfs2.h"
  34. #include "alloc.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "file.h"
  38. #include "heartbeat.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 "buffer_head_io.h"
  48. struct ocfs2_find_inode_args
  49. {
  50. u64 fi_blkno;
  51. unsigned long fi_ino;
  52. unsigned int fi_flags;
  53. unsigned int fi_sysfile_type;
  54. };
  55. static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
  56. static int ocfs2_read_locked_inode(struct inode *inode,
  57. struct ocfs2_find_inode_args *args);
  58. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  59. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  60. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  61. struct inode *inode,
  62. struct buffer_head *fe_bh);
  63. void ocfs2_set_inode_flags(struct inode *inode)
  64. {
  65. unsigned int flags = OCFS2_I(inode)->ip_attr;
  66. inode->i_flags &= ~(S_IMMUTABLE |
  67. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  68. if (flags & OCFS2_IMMUTABLE_FL)
  69. inode->i_flags |= S_IMMUTABLE;
  70. if (flags & OCFS2_SYNC_FL)
  71. inode->i_flags |= S_SYNC;
  72. if (flags & OCFS2_APPEND_FL)
  73. inode->i_flags |= S_APPEND;
  74. if (flags & OCFS2_NOATIME_FL)
  75. inode->i_flags |= S_NOATIME;
  76. if (flags & OCFS2_DIRSYNC_FL)
  77. inode->i_flags |= S_DIRSYNC;
  78. }
  79. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  80. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  81. {
  82. unsigned int flags = oi->vfs_inode.i_flags;
  83. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  84. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  85. if (flags & S_SYNC)
  86. oi->ip_attr |= OCFS2_SYNC_FL;
  87. if (flags & S_APPEND)
  88. oi->ip_attr |= OCFS2_APPEND_FL;
  89. if (flags & S_IMMUTABLE)
  90. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  91. if (flags & S_NOATIME)
  92. oi->ip_attr |= OCFS2_NOATIME_FL;
  93. if (flags & S_DIRSYNC)
  94. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  95. }
  96. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  97. int sysfile_type)
  98. {
  99. struct inode *inode = NULL;
  100. struct super_block *sb = osb->sb;
  101. struct ocfs2_find_inode_args args;
  102. mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
  103. /* Ok. By now we've either got the offsets passed to us by the
  104. * caller, or we just pulled them off the bh. Lets do some
  105. * sanity checks to make sure they're OK. */
  106. if (blkno == 0) {
  107. inode = ERR_PTR(-EINVAL);
  108. mlog_errno(PTR_ERR(inode));
  109. goto bail;
  110. }
  111. args.fi_blkno = blkno;
  112. args.fi_flags = flags;
  113. args.fi_ino = ino_from_blkno(sb, blkno);
  114. args.fi_sysfile_type = sysfile_type;
  115. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  116. ocfs2_init_locked_inode, &args);
  117. /* inode was *not* in the inode cache. 2.6.x requires
  118. * us to do our own read_inode call and unlock it
  119. * afterwards. */
  120. if (inode && inode->i_state & I_NEW) {
  121. mlog(0, "Inode was not in inode cache, reading it.\n");
  122. ocfs2_read_locked_inode(inode, &args);
  123. unlock_new_inode(inode);
  124. }
  125. if (inode == NULL) {
  126. inode = ERR_PTR(-ENOMEM);
  127. mlog_errno(PTR_ERR(inode));
  128. goto bail;
  129. }
  130. if (is_bad_inode(inode)) {
  131. iput(inode);
  132. inode = ERR_PTR(-ESTALE);
  133. goto bail;
  134. }
  135. bail:
  136. if (!IS_ERR(inode)) {
  137. mlog(0, "returning inode with number %llu\n",
  138. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  139. mlog_exit_ptr(inode);
  140. }
  141. return inode;
  142. }
  143. /*
  144. * here's how inodes get read from disk:
  145. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  146. * found? : return the in-memory inode
  147. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  148. */
  149. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  150. {
  151. struct ocfs2_find_inode_args *args = NULL;
  152. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  153. int ret = 0;
  154. mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
  155. args = opaque;
  156. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  157. if (oi->ip_blkno != args->fi_blkno)
  158. goto bail;
  159. ret = 1;
  160. bail:
  161. mlog_exit(ret);
  162. return ret;
  163. }
  164. /*
  165. * initialize the new inode, but don't do anything that would cause
  166. * us to sleep.
  167. * return 0 on success, 1 on failure
  168. */
  169. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  170. {
  171. struct ocfs2_find_inode_args *args = opaque;
  172. mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
  173. inode->i_ino = args->fi_ino;
  174. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  175. if (args->fi_sysfile_type != 0)
  176. lockdep_set_class(&inode->i_mutex,
  177. &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
  178. mlog_exit(0);
  179. return 0;
  180. }
  181. int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  182. int create_ino)
  183. {
  184. struct super_block *sb;
  185. struct ocfs2_super *osb;
  186. int status = -EINVAL;
  187. int use_plocks = 1;
  188. mlog_entry("(0x%p, size:%llu)\n", inode,
  189. (unsigned long long)le64_to_cpu(fe->i_size));
  190. sb = inode->i_sb;
  191. osb = OCFS2_SB(sb);
  192. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  193. ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  194. use_plocks = 0;
  195. /* this means that read_inode cannot create a superblock inode
  196. * today. change if needed. */
  197. if (!OCFS2_IS_VALID_DINODE(fe) ||
  198. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  199. mlog(0, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
  200. "signature = %.*s, flags = 0x%x\n",
  201. inode->i_ino,
  202. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  203. fe->i_signature, le32_to_cpu(fe->i_flags));
  204. goto bail;
  205. }
  206. if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
  207. mlog(ML_ERROR, "file entry generation does not match "
  208. "superblock! osb->fs_generation=%x, "
  209. "fe->i_fs_generation=%x\n",
  210. osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
  211. goto bail;
  212. }
  213. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  214. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  215. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  216. inode->i_version = 1;
  217. inode->i_generation = le32_to_cpu(fe->i_generation);
  218. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  219. inode->i_mode = le16_to_cpu(fe->i_mode);
  220. inode->i_uid = le32_to_cpu(fe->i_uid);
  221. inode->i_gid = le32_to_cpu(fe->i_gid);
  222. /* Fast symlinks will have i_size but no allocated clusters. */
  223. if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
  224. inode->i_blocks = 0;
  225. else
  226. inode->i_blocks = ocfs2_inode_sector_count(inode);
  227. inode->i_mapping->a_ops = &ocfs2_aops;
  228. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  229. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  230. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  231. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  232. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  233. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  234. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  235. mlog(ML_ERROR,
  236. "ip_blkno %llu != i_blkno %llu!\n",
  237. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  238. (unsigned long long)le64_to_cpu(fe->i_blkno));
  239. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  240. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
  241. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  242. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  243. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  244. mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
  245. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  246. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  247. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  248. mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
  249. /* we can't actually hit this as read_inode can't
  250. * handle superblocks today ;-) */
  251. BUG();
  252. }
  253. switch (inode->i_mode & S_IFMT) {
  254. case S_IFREG:
  255. if (use_plocks)
  256. inode->i_fop = &ocfs2_fops;
  257. else
  258. inode->i_fop = &ocfs2_fops_no_plocks;
  259. inode->i_op = &ocfs2_file_iops;
  260. i_size_write(inode, le64_to_cpu(fe->i_size));
  261. break;
  262. case S_IFDIR:
  263. inode->i_op = &ocfs2_dir_iops;
  264. if (use_plocks)
  265. inode->i_fop = &ocfs2_dops;
  266. else
  267. inode->i_fop = &ocfs2_dops_no_plocks;
  268. i_size_write(inode, le64_to_cpu(fe->i_size));
  269. break;
  270. case S_IFLNK:
  271. if (ocfs2_inode_is_fast_symlink(inode))
  272. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  273. else
  274. inode->i_op = &ocfs2_symlink_inode_operations;
  275. i_size_write(inode, le64_to_cpu(fe->i_size));
  276. break;
  277. default:
  278. inode->i_op = &ocfs2_special_file_iops;
  279. init_special_inode(inode, inode->i_mode,
  280. inode->i_rdev);
  281. break;
  282. }
  283. if (create_ino) {
  284. inode->i_ino = ino_from_blkno(inode->i_sb,
  285. le64_to_cpu(fe->i_blkno));
  286. /*
  287. * If we ever want to create system files from kernel,
  288. * the generation argument to
  289. * ocfs2_inode_lock_res_init() will have to change.
  290. */
  291. BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
  292. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  293. OCFS2_LOCK_TYPE_META, 0, inode);
  294. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  295. OCFS2_LOCK_TYPE_OPEN, 0, inode);
  296. }
  297. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  298. OCFS2_LOCK_TYPE_RW, inode->i_generation,
  299. inode);
  300. ocfs2_set_inode_flags(inode);
  301. status = 0;
  302. bail:
  303. mlog_exit(status);
  304. return status;
  305. }
  306. static int ocfs2_read_locked_inode(struct inode *inode,
  307. struct ocfs2_find_inode_args *args)
  308. {
  309. struct super_block *sb;
  310. struct ocfs2_super *osb;
  311. struct ocfs2_dinode *fe;
  312. struct buffer_head *bh = NULL;
  313. int status, can_lock;
  314. u32 generation = 0;
  315. mlog_entry("(0x%p, 0x%p)\n", inode, args);
  316. status = -EINVAL;
  317. if (inode == NULL || inode->i_sb == NULL) {
  318. mlog(ML_ERROR, "bad inode\n");
  319. return status;
  320. }
  321. sb = inode->i_sb;
  322. osb = OCFS2_SB(sb);
  323. if (!args) {
  324. mlog(ML_ERROR, "bad inode args\n");
  325. make_bad_inode(inode);
  326. return status;
  327. }
  328. /*
  329. * To improve performance of cold-cache inode stats, we take
  330. * the cluster lock here if possible.
  331. *
  332. * Generally, OCFS2 never trusts the contents of an inode
  333. * unless it's holding a cluster lock, so taking it here isn't
  334. * a correctness issue as much as it is a performance
  335. * improvement.
  336. *
  337. * There are three times when taking the lock is not a good idea:
  338. *
  339. * 1) During startup, before we have initialized the DLM.
  340. *
  341. * 2) If we are reading certain system files which never get
  342. * cluster locks (local alloc, truncate log).
  343. *
  344. * 3) If the process doing the iget() is responsible for
  345. * orphan dir recovery. We're holding the orphan dir lock and
  346. * can get into a deadlock with another process on another
  347. * node in ->delete_inode().
  348. *
  349. * #1 and #2 can be simply solved by never taking the lock
  350. * here for system files (which are the only type we read
  351. * during mount). It's a heavier approach, but our main
  352. * concern is user-accesible files anyway.
  353. *
  354. * #3 works itself out because we'll eventually take the
  355. * cluster lock before trusting anything anyway.
  356. */
  357. can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  358. && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
  359. && !ocfs2_mount_local(osb);
  360. /*
  361. * To maintain backwards compatibility with older versions of
  362. * ocfs2-tools, we still store the generation value for system
  363. * files. The only ones that actually matter to userspace are
  364. * the journals, but it's easier and inexpensive to just flag
  365. * all system files similarly.
  366. */
  367. if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  368. generation = osb->fs_generation;
  369. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  370. OCFS2_LOCK_TYPE_META,
  371. generation, inode);
  372. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  373. OCFS2_LOCK_TYPE_OPEN,
  374. 0, inode);
  375. if (can_lock) {
  376. status = ocfs2_open_lock(inode);
  377. if (status) {
  378. make_bad_inode(inode);
  379. mlog_errno(status);
  380. return status;
  381. }
  382. status = ocfs2_inode_lock(inode, NULL, 0);
  383. if (status) {
  384. make_bad_inode(inode);
  385. mlog_errno(status);
  386. return status;
  387. }
  388. }
  389. if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  390. status = ocfs2_try_open_lock(inode, 0);
  391. if (status) {
  392. make_bad_inode(inode);
  393. return status;
  394. }
  395. }
  396. status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0,
  397. can_lock ? inode : NULL);
  398. if (status < 0) {
  399. mlog_errno(status);
  400. goto bail;
  401. }
  402. status = -EINVAL;
  403. fe = (struct ocfs2_dinode *) bh->b_data;
  404. if (!OCFS2_IS_VALID_DINODE(fe)) {
  405. mlog(0, "Invalid dinode #%llu: signature = %.*s\n",
  406. (unsigned long long)args->fi_blkno, 7,
  407. fe->i_signature);
  408. goto bail;
  409. }
  410. /*
  411. * This is a code bug. Right now the caller needs to
  412. * understand whether it is asking for a system file inode or
  413. * not so the proper lock names can be built.
  414. */
  415. mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  416. !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  417. "Inode %llu: system file state is ambigous\n",
  418. (unsigned long long)args->fi_blkno);
  419. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  420. S_ISBLK(le16_to_cpu(fe->i_mode)))
  421. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  422. if (ocfs2_populate_inode(inode, fe, 0) < 0)
  423. goto bail;
  424. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  425. status = 0;
  426. bail:
  427. if (can_lock)
  428. ocfs2_inode_unlock(inode, 0);
  429. if (status < 0)
  430. make_bad_inode(inode);
  431. if (args && bh)
  432. brelse(bh);
  433. mlog_exit(status);
  434. return status;
  435. }
  436. void ocfs2_sync_blockdev(struct super_block *sb)
  437. {
  438. sync_blockdev(sb->s_bdev);
  439. }
  440. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  441. struct inode *inode,
  442. struct buffer_head *fe_bh)
  443. {
  444. int status = 0;
  445. struct ocfs2_truncate_context *tc = NULL;
  446. struct ocfs2_dinode *fe;
  447. handle_t *handle = NULL;
  448. mlog_entry_void();
  449. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  450. /*
  451. * This check will also skip truncate of inodes with inline
  452. * data and fast symlinks.
  453. */
  454. if (fe->i_clusters) {
  455. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  456. if (IS_ERR(handle)) {
  457. status = PTR_ERR(handle);
  458. mlog_errno(status);
  459. goto out;
  460. }
  461. status = ocfs2_journal_access(handle, inode, fe_bh,
  462. OCFS2_JOURNAL_ACCESS_WRITE);
  463. if (status < 0) {
  464. mlog_errno(status);
  465. goto out;
  466. }
  467. i_size_write(inode, 0);
  468. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  469. if (status < 0) {
  470. mlog_errno(status);
  471. goto out;
  472. }
  473. ocfs2_commit_trans(osb, handle);
  474. handle = NULL;
  475. status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
  476. if (status < 0) {
  477. mlog_errno(status);
  478. goto out;
  479. }
  480. status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
  481. if (status < 0) {
  482. mlog_errno(status);
  483. goto out;
  484. }
  485. }
  486. out:
  487. if (handle)
  488. ocfs2_commit_trans(osb, handle);
  489. mlog_exit(status);
  490. return status;
  491. }
  492. static int ocfs2_remove_inode(struct inode *inode,
  493. struct buffer_head *di_bh,
  494. struct inode *orphan_dir_inode,
  495. struct buffer_head *orphan_dir_bh)
  496. {
  497. int status;
  498. struct inode *inode_alloc_inode = NULL;
  499. struct buffer_head *inode_alloc_bh = NULL;
  500. handle_t *handle;
  501. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  502. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  503. inode_alloc_inode =
  504. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  505. le16_to_cpu(di->i_suballoc_slot));
  506. if (!inode_alloc_inode) {
  507. status = -EEXIST;
  508. mlog_errno(status);
  509. goto bail;
  510. }
  511. mutex_lock(&inode_alloc_inode->i_mutex);
  512. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  513. if (status < 0) {
  514. mutex_unlock(&inode_alloc_inode->i_mutex);
  515. mlog_errno(status);
  516. goto bail;
  517. }
  518. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS);
  519. if (IS_ERR(handle)) {
  520. status = PTR_ERR(handle);
  521. mlog_errno(status);
  522. goto bail_unlock;
  523. }
  524. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  525. orphan_dir_bh);
  526. if (status < 0) {
  527. mlog_errno(status);
  528. goto bail_commit;
  529. }
  530. /* set the inodes dtime */
  531. status = ocfs2_journal_access(handle, inode, di_bh,
  532. OCFS2_JOURNAL_ACCESS_WRITE);
  533. if (status < 0) {
  534. mlog_errno(status);
  535. goto bail_commit;
  536. }
  537. di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
  538. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  539. status = ocfs2_journal_dirty(handle, di_bh);
  540. if (status < 0) {
  541. mlog_errno(status);
  542. goto bail_commit;
  543. }
  544. ocfs2_remove_from_cache(inode, di_bh);
  545. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  546. inode_alloc_bh, di);
  547. if (status < 0)
  548. mlog_errno(status);
  549. bail_commit:
  550. ocfs2_commit_trans(osb, handle);
  551. bail_unlock:
  552. ocfs2_inode_unlock(inode_alloc_inode, 1);
  553. mutex_unlock(&inode_alloc_inode->i_mutex);
  554. brelse(inode_alloc_bh);
  555. bail:
  556. iput(inode_alloc_inode);
  557. return status;
  558. }
  559. /*
  560. * Serialize with orphan dir recovery. If the process doing
  561. * recovery on this orphan dir does an iget() with the dir
  562. * i_mutex held, we'll deadlock here. Instead we detect this
  563. * and exit early - recovery will wipe this inode for us.
  564. */
  565. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  566. int slot)
  567. {
  568. int ret = 0;
  569. spin_lock(&osb->osb_lock);
  570. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  571. mlog(0, "Recovery is happening on orphan dir %d, will skip "
  572. "this inode\n", slot);
  573. ret = -EDEADLK;
  574. goto out;
  575. }
  576. /* This signals to the orphan recovery process that it should
  577. * wait for us to handle the wipe. */
  578. osb->osb_orphan_wipes[slot]++;
  579. out:
  580. spin_unlock(&osb->osb_lock);
  581. return ret;
  582. }
  583. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  584. int slot)
  585. {
  586. spin_lock(&osb->osb_lock);
  587. osb->osb_orphan_wipes[slot]--;
  588. spin_unlock(&osb->osb_lock);
  589. wake_up(&osb->osb_wipe_event);
  590. }
  591. static int ocfs2_wipe_inode(struct inode *inode,
  592. struct buffer_head *di_bh)
  593. {
  594. int status, orphaned_slot;
  595. struct inode *orphan_dir_inode = NULL;
  596. struct buffer_head *orphan_dir_bh = NULL;
  597. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  598. struct ocfs2_dinode *di;
  599. di = (struct ocfs2_dinode *) di_bh->b_data;
  600. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  601. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  602. if (status)
  603. return status;
  604. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  605. ORPHAN_DIR_SYSTEM_INODE,
  606. orphaned_slot);
  607. if (!orphan_dir_inode) {
  608. status = -EEXIST;
  609. mlog_errno(status);
  610. goto bail;
  611. }
  612. /* Lock the orphan dir. The lock will be held for the entire
  613. * delete_inode operation. We do this now to avoid races with
  614. * recovery completion on other nodes. */
  615. mutex_lock(&orphan_dir_inode->i_mutex);
  616. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  617. if (status < 0) {
  618. mutex_unlock(&orphan_dir_inode->i_mutex);
  619. mlog_errno(status);
  620. goto bail;
  621. }
  622. /* we do this while holding the orphan dir lock because we
  623. * don't want recovery being run from another node to try an
  624. * inode delete underneath us -- this will result in two nodes
  625. * truncating the same file! */
  626. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto bail_unlock_dir;
  630. }
  631. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  632. orphan_dir_bh);
  633. if (status < 0)
  634. mlog_errno(status);
  635. bail_unlock_dir:
  636. ocfs2_inode_unlock(orphan_dir_inode, 1);
  637. mutex_unlock(&orphan_dir_inode->i_mutex);
  638. brelse(orphan_dir_bh);
  639. bail:
  640. iput(orphan_dir_inode);
  641. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  642. return status;
  643. }
  644. /* There is a series of simple checks that should be done before a
  645. * trylock is even considered. Encapsulate those in this function. */
  646. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  647. {
  648. int ret = 0;
  649. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  650. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  651. /* We shouldn't be getting here for the root directory
  652. * inode.. */
  653. if (inode == osb->root_inode) {
  654. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  655. goto bail;
  656. }
  657. /* If we're coming from downconvert_thread we can't go into our own
  658. * voting [hello, deadlock city!], so unforuntately we just
  659. * have to skip deleting this guy. That's OK though because
  660. * the node who's doing the actual deleting should handle it
  661. * anyway. */
  662. if (current == osb->dc_task) {
  663. mlog(0, "Skipping delete of %lu because we're currently "
  664. "in downconvert\n", inode->i_ino);
  665. goto bail;
  666. }
  667. spin_lock(&oi->ip_lock);
  668. /* OCFS2 *never* deletes system files. This should technically
  669. * never get here as system file inodes should always have a
  670. * positive link count. */
  671. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  672. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  673. (unsigned long long)oi->ip_blkno);
  674. goto bail_unlock;
  675. }
  676. /* If we have allowd wipe of this inode for another node, it
  677. * will be marked here so we can safely skip it. Recovery will
  678. * cleanup any inodes we might inadvertantly skip here. */
  679. if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
  680. mlog(0, "Skipping delete of %lu because another node "
  681. "has done this for us.\n", inode->i_ino);
  682. goto bail_unlock;
  683. }
  684. ret = 1;
  685. bail_unlock:
  686. spin_unlock(&oi->ip_lock);
  687. bail:
  688. return ret;
  689. }
  690. /* Query the cluster to determine whether we should wipe an inode from
  691. * disk or not.
  692. *
  693. * Requires the inode to have the cluster lock. */
  694. static int ocfs2_query_inode_wipe(struct inode *inode,
  695. struct buffer_head *di_bh,
  696. int *wipe)
  697. {
  698. int status = 0;
  699. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  700. struct ocfs2_dinode *di;
  701. *wipe = 0;
  702. /* While we were waiting for the cluster lock in
  703. * ocfs2_delete_inode, another node might have asked to delete
  704. * the inode. Recheck our flags to catch this. */
  705. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  706. mlog(0, "Skipping delete of %llu because flags changed\n",
  707. (unsigned long long)oi->ip_blkno);
  708. goto bail;
  709. }
  710. /* Now that we have an up to date inode, we can double check
  711. * the link count. */
  712. if (inode->i_nlink) {
  713. mlog(0, "Skipping delete of %llu because nlink = %u\n",
  714. (unsigned long long)oi->ip_blkno, inode->i_nlink);
  715. goto bail;
  716. }
  717. /* Do some basic inode verification... */
  718. di = (struct ocfs2_dinode *) di_bh->b_data;
  719. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
  720. /* for lack of a better error? */
  721. status = -EEXIST;
  722. mlog(ML_ERROR,
  723. "Inode %llu (on-disk %llu) not orphaned! "
  724. "Disk flags 0x%x, inode flags 0x%x\n",
  725. (unsigned long long)oi->ip_blkno,
  726. (unsigned long long)le64_to_cpu(di->i_blkno),
  727. le32_to_cpu(di->i_flags), oi->ip_flags);
  728. goto bail;
  729. }
  730. /* has someone already deleted us?! baaad... */
  731. if (di->i_dtime) {
  732. status = -EEXIST;
  733. mlog_errno(status);
  734. goto bail;
  735. }
  736. /*
  737. * This is how ocfs2 determines whether an inode is still live
  738. * within the cluster. Every node takes a shared read lock on
  739. * the inode open lock in ocfs2_read_locked_inode(). When we
  740. * get to ->delete_inode(), each node tries to convert it's
  741. * lock to an exclusive. Trylocks are serialized by the inode
  742. * meta data lock. If the upconvert suceeds, we know the inode
  743. * is no longer live and can be deleted.
  744. *
  745. * Though we call this with the meta data lock held, the
  746. * trylock keeps us from ABBA deadlock.
  747. */
  748. status = ocfs2_try_open_lock(inode, 1);
  749. if (status == -EAGAIN) {
  750. status = 0;
  751. mlog(0, "Skipping delete of %llu because it is in use on "
  752. "other nodes\n", (unsigned long long)oi->ip_blkno);
  753. goto bail;
  754. }
  755. if (status < 0) {
  756. mlog_errno(status);
  757. goto bail;
  758. }
  759. *wipe = 1;
  760. mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
  761. (unsigned long long)oi->ip_blkno,
  762. le16_to_cpu(di->i_orphaned_slot));
  763. bail:
  764. return status;
  765. }
  766. /* Support function for ocfs2_delete_inode. Will help us keep the
  767. * inode data in a consistent state for clear_inode. Always truncates
  768. * pages, optionally sync's them first. */
  769. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  770. int sync_data)
  771. {
  772. mlog(0, "Cleanup inode %llu, sync = %d\n",
  773. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  774. if (sync_data)
  775. write_inode_now(inode, 1);
  776. truncate_inode_pages(&inode->i_data, 0);
  777. }
  778. void ocfs2_delete_inode(struct inode *inode)
  779. {
  780. int wipe, status;
  781. sigset_t blocked, oldset;
  782. struct buffer_head *di_bh = NULL;
  783. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  784. if (is_bad_inode(inode)) {
  785. mlog(0, "Skipping delete of bad inode\n");
  786. goto bail;
  787. }
  788. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  789. /* It's probably not necessary to truncate_inode_pages
  790. * here but we do it for safety anyway (it will most
  791. * likely be a no-op anyway) */
  792. ocfs2_cleanup_delete_inode(inode, 0);
  793. goto bail;
  794. }
  795. /* We want to block signals in delete_inode as the lock and
  796. * messaging paths may return us -ERESTARTSYS. Which would
  797. * cause us to exit early, resulting in inodes being orphaned
  798. * forever. */
  799. sigfillset(&blocked);
  800. status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
  801. if (status < 0) {
  802. mlog_errno(status);
  803. ocfs2_cleanup_delete_inode(inode, 1);
  804. goto bail;
  805. }
  806. /* Lock down the inode. This gives us an up to date view of
  807. * it's metadata (for verification), and allows us to
  808. * serialize delete_inode on multiple nodes.
  809. *
  810. * Even though we might be doing a truncate, we don't take the
  811. * allocation lock here as it won't be needed - nobody will
  812. * have the file open.
  813. */
  814. status = ocfs2_inode_lock(inode, &di_bh, 1);
  815. if (status < 0) {
  816. if (status != -ENOENT)
  817. mlog_errno(status);
  818. ocfs2_cleanup_delete_inode(inode, 0);
  819. goto bail_unblock;
  820. }
  821. /* Query the cluster. This will be the final decision made
  822. * before we go ahead and wipe the inode. */
  823. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  824. if (!wipe || status < 0) {
  825. /* Error and remote inode busy both mean we won't be
  826. * removing the inode, so they take almost the same
  827. * path. */
  828. if (status < 0)
  829. mlog_errno(status);
  830. /* Someone in the cluster has disallowed a wipe of
  831. * this inode, or it was never completely
  832. * orphaned. Write out the pages and exit now. */
  833. ocfs2_cleanup_delete_inode(inode, 1);
  834. goto bail_unlock_inode;
  835. }
  836. ocfs2_cleanup_delete_inode(inode, 0);
  837. status = ocfs2_wipe_inode(inode, di_bh);
  838. if (status < 0) {
  839. if (status != -EDEADLK)
  840. mlog_errno(status);
  841. goto bail_unlock_inode;
  842. }
  843. /*
  844. * Mark the inode as successfully deleted.
  845. *
  846. * This is important for ocfs2_clear_inode() as it will check
  847. * this flag and skip any checkpointing work
  848. *
  849. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  850. * the LVB for other nodes.
  851. */
  852. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  853. bail_unlock_inode:
  854. ocfs2_inode_unlock(inode, 1);
  855. brelse(di_bh);
  856. bail_unblock:
  857. status = sigprocmask(SIG_SETMASK, &oldset, NULL);
  858. if (status < 0)
  859. mlog_errno(status);
  860. bail:
  861. clear_inode(inode);
  862. mlog_exit_void();
  863. }
  864. void ocfs2_clear_inode(struct inode *inode)
  865. {
  866. int status;
  867. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  868. mlog_entry_void();
  869. if (!inode)
  870. goto bail;
  871. mlog(0, "Clearing inode: %llu, nlink = %u\n",
  872. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
  873. mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  874. "Inode=%lu\n", inode->i_ino);
  875. /* To preven remote deletes we hold open lock before, now it
  876. * is time to unlock PR and EX open locks. */
  877. ocfs2_open_unlock(inode);
  878. /* Do these before all the other work so that we don't bounce
  879. * the downconvert thread while waiting to destroy the locks. */
  880. ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
  881. ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
  882. ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
  883. /* We very well may get a clear_inode before all an inodes
  884. * metadata has hit disk. Of course, we can't drop any cluster
  885. * locks until the journal has finished with it. The only
  886. * exception here are successfully wiped inodes - their
  887. * metadata can now be considered to be part of the system
  888. * inodes from which it came. */
  889. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  890. ocfs2_checkpoint_inode(inode);
  891. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  892. "Clear inode of %llu, inode has io markers\n",
  893. (unsigned long long)oi->ip_blkno);
  894. ocfs2_extent_map_trunc(inode, 0);
  895. status = ocfs2_drop_inode_locks(inode);
  896. if (status < 0)
  897. mlog_errno(status);
  898. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  899. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  900. ocfs2_lock_res_free(&oi->ip_open_lockres);
  901. ocfs2_metadata_cache_purge(inode);
  902. mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
  903. "Clear inode of %llu, inode has %u cache items\n",
  904. (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
  905. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  906. "Clear inode of %llu, inode has a bad flag\n",
  907. (unsigned long long)oi->ip_blkno);
  908. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  909. "Clear inode of %llu, inode is locked\n",
  910. (unsigned long long)oi->ip_blkno);
  911. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  912. "Clear inode of %llu, io_mutex is locked\n",
  913. (unsigned long long)oi->ip_blkno);
  914. mutex_unlock(&oi->ip_io_mutex);
  915. /*
  916. * down_trylock() returns 0, down_write_trylock() returns 1
  917. * kernel 1, world 0
  918. */
  919. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  920. "Clear inode of %llu, alloc_sem is locked\n",
  921. (unsigned long long)oi->ip_blkno);
  922. up_write(&oi->ip_alloc_sem);
  923. mlog_bug_on_msg(oi->ip_open_count,
  924. "Clear inode of %llu has open count %d\n",
  925. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  926. /* Clear all other flags. */
  927. oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
  928. oi->ip_created_trans = 0;
  929. oi->ip_last_trans = 0;
  930. oi->ip_dir_start_lookup = 0;
  931. oi->ip_blkno = 0ULL;
  932. bail:
  933. mlog_exit_void();
  934. }
  935. /* Called under inode_lock, with no more references on the
  936. * struct inode, so it's safe here to check the flags field
  937. * and to manipulate i_nlink without any other locks. */
  938. void ocfs2_drop_inode(struct inode *inode)
  939. {
  940. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  941. mlog_entry_void();
  942. mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
  943. (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
  944. if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
  945. generic_delete_inode(inode);
  946. else
  947. generic_drop_inode(inode);
  948. mlog_exit_void();
  949. }
  950. /*
  951. * TODO: this should probably be merged into ocfs2_get_block
  952. *
  953. * However, you now need to pay attention to the cont_prepare_write()
  954. * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
  955. * expects never to extend).
  956. */
  957. struct buffer_head *ocfs2_bread(struct inode *inode,
  958. int block, int *err, int reada)
  959. {
  960. struct buffer_head *bh = NULL;
  961. int tmperr;
  962. u64 p_blkno;
  963. int readflags = OCFS2_BH_CACHED;
  964. if (reada)
  965. readflags |= OCFS2_BH_READAHEAD;
  966. if (((u64)block << inode->i_sb->s_blocksize_bits) >=
  967. i_size_read(inode)) {
  968. BUG_ON(!reada);
  969. return NULL;
  970. }
  971. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  972. tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
  973. NULL);
  974. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  975. if (tmperr < 0) {
  976. mlog_errno(tmperr);
  977. goto fail;
  978. }
  979. tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
  980. readflags, inode);
  981. if (tmperr < 0)
  982. goto fail;
  983. tmperr = 0;
  984. *err = 0;
  985. return bh;
  986. fail:
  987. if (bh) {
  988. brelse(bh);
  989. bh = NULL;
  990. }
  991. *err = -EIO;
  992. return NULL;
  993. }
  994. /*
  995. * This is called from our getattr.
  996. */
  997. int ocfs2_inode_revalidate(struct dentry *dentry)
  998. {
  999. struct inode *inode = dentry->d_inode;
  1000. int status = 0;
  1001. mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
  1002. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
  1003. if (!inode) {
  1004. mlog(0, "eep, no inode!\n");
  1005. status = -ENOENT;
  1006. goto bail;
  1007. }
  1008. spin_lock(&OCFS2_I(inode)->ip_lock);
  1009. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  1010. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1011. mlog(0, "inode deleted!\n");
  1012. status = -ENOENT;
  1013. goto bail;
  1014. }
  1015. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1016. /* Let ocfs2_inode_lock do the work of updating our struct
  1017. * inode for us. */
  1018. status = ocfs2_inode_lock(inode, NULL, 0);
  1019. if (status < 0) {
  1020. if (status != -ENOENT)
  1021. mlog_errno(status);
  1022. goto bail;
  1023. }
  1024. ocfs2_inode_unlock(inode, 0);
  1025. bail:
  1026. mlog_exit(status);
  1027. return status;
  1028. }
  1029. /*
  1030. * Updates a disk inode from a
  1031. * struct inode.
  1032. * Only takes ip_lock.
  1033. */
  1034. int ocfs2_mark_inode_dirty(handle_t *handle,
  1035. struct inode *inode,
  1036. struct buffer_head *bh)
  1037. {
  1038. int status;
  1039. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1040. mlog_entry("(inode %llu)\n",
  1041. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1042. status = ocfs2_journal_access(handle, inode, bh,
  1043. OCFS2_JOURNAL_ACCESS_WRITE);
  1044. if (status < 0) {
  1045. mlog_errno(status);
  1046. goto leave;
  1047. }
  1048. spin_lock(&OCFS2_I(inode)->ip_lock);
  1049. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1050. ocfs2_get_inode_flags(OCFS2_I(inode));
  1051. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1052. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1053. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1054. fe->i_size = cpu_to_le64(i_size_read(inode));
  1055. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  1056. fe->i_uid = cpu_to_le32(inode->i_uid);
  1057. fe->i_gid = cpu_to_le32(inode->i_gid);
  1058. fe->i_mode = cpu_to_le16(inode->i_mode);
  1059. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1060. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1061. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1062. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1063. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1064. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1065. status = ocfs2_journal_dirty(handle, bh);
  1066. if (status < 0)
  1067. mlog_errno(status);
  1068. status = 0;
  1069. leave:
  1070. mlog_exit(status);
  1071. return status;
  1072. }
  1073. /*
  1074. *
  1075. * Updates a struct inode from a disk inode.
  1076. * does no i/o, only takes ip_lock.
  1077. */
  1078. void ocfs2_refresh_inode(struct inode *inode,
  1079. struct ocfs2_dinode *fe)
  1080. {
  1081. spin_lock(&OCFS2_I(inode)->ip_lock);
  1082. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1083. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1084. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1085. ocfs2_set_inode_flags(inode);
  1086. i_size_write(inode, le64_to_cpu(fe->i_size));
  1087. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  1088. inode->i_uid = le32_to_cpu(fe->i_uid);
  1089. inode->i_gid = le32_to_cpu(fe->i_gid);
  1090. inode->i_mode = le16_to_cpu(fe->i_mode);
  1091. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1092. inode->i_blocks = 0;
  1093. else
  1094. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1095. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1096. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1097. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1098. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1099. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1100. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1101. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1102. }