inode.c 35 KB

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