inode.c 40 KB

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