inode.c 46 KB

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