inode.c 39 KB

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