inode.c 39 KB

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