inode.c 35 KB

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