inode.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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. }
  553. out:
  554. if (handle)
  555. ocfs2_commit_trans(osb, handle);
  556. return status;
  557. }
  558. static int ocfs2_remove_inode(struct inode *inode,
  559. struct buffer_head *di_bh,
  560. struct inode *orphan_dir_inode,
  561. struct buffer_head *orphan_dir_bh)
  562. {
  563. int status;
  564. struct inode *inode_alloc_inode = NULL;
  565. struct buffer_head *inode_alloc_bh = NULL;
  566. handle_t *handle;
  567. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  568. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  569. inode_alloc_inode =
  570. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  571. le16_to_cpu(di->i_suballoc_slot));
  572. if (!inode_alloc_inode) {
  573. status = -ENOENT;
  574. mlog_errno(status);
  575. goto bail;
  576. }
  577. inode_lock(inode_alloc_inode);
  578. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  579. if (status < 0) {
  580. inode_unlock(inode_alloc_inode);
  581. mlog_errno(status);
  582. goto bail;
  583. }
  584. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
  585. ocfs2_quota_trans_credits(inode->i_sb));
  586. if (IS_ERR(handle)) {
  587. status = PTR_ERR(handle);
  588. mlog_errno(status);
  589. goto bail_unlock;
  590. }
  591. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  592. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  593. orphan_dir_bh, false);
  594. if (status < 0) {
  595. mlog_errno(status);
  596. goto bail_commit;
  597. }
  598. }
  599. /* set the inodes dtime */
  600. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  601. OCFS2_JOURNAL_ACCESS_WRITE);
  602. if (status < 0) {
  603. mlog_errno(status);
  604. goto bail_commit;
  605. }
  606. di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
  607. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  608. ocfs2_journal_dirty(handle, di_bh);
  609. ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
  610. dquot_free_inode(inode);
  611. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  612. inode_alloc_bh, di);
  613. if (status < 0)
  614. mlog_errno(status);
  615. bail_commit:
  616. ocfs2_commit_trans(osb, handle);
  617. bail_unlock:
  618. ocfs2_inode_unlock(inode_alloc_inode, 1);
  619. inode_unlock(inode_alloc_inode);
  620. brelse(inode_alloc_bh);
  621. bail:
  622. iput(inode_alloc_inode);
  623. return status;
  624. }
  625. /*
  626. * Serialize with orphan dir recovery. If the process doing
  627. * recovery on this orphan dir does an iget() with the dir
  628. * i_mutex held, we'll deadlock here. Instead we detect this
  629. * and exit early - recovery will wipe this inode for us.
  630. */
  631. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  632. int slot)
  633. {
  634. int ret = 0;
  635. spin_lock(&osb->osb_lock);
  636. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  637. ret = -EDEADLK;
  638. goto out;
  639. }
  640. /* This signals to the orphan recovery process that it should
  641. * wait for us to handle the wipe. */
  642. osb->osb_orphan_wipes[slot]++;
  643. out:
  644. spin_unlock(&osb->osb_lock);
  645. trace_ocfs2_check_orphan_recovery_state(slot, ret);
  646. return ret;
  647. }
  648. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  649. int slot)
  650. {
  651. spin_lock(&osb->osb_lock);
  652. osb->osb_orphan_wipes[slot]--;
  653. spin_unlock(&osb->osb_lock);
  654. wake_up(&osb->osb_wipe_event);
  655. }
  656. static int ocfs2_wipe_inode(struct inode *inode,
  657. struct buffer_head *di_bh)
  658. {
  659. int status, orphaned_slot = -1;
  660. struct inode *orphan_dir_inode = NULL;
  661. struct buffer_head *orphan_dir_bh = NULL;
  662. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  663. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  664. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  665. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  666. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  667. if (status)
  668. return status;
  669. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  670. ORPHAN_DIR_SYSTEM_INODE,
  671. orphaned_slot);
  672. if (!orphan_dir_inode) {
  673. status = -ENOENT;
  674. mlog_errno(status);
  675. goto bail;
  676. }
  677. /* Lock the orphan dir. The lock will be held for the entire
  678. * delete_inode operation. We do this now to avoid races with
  679. * recovery completion on other nodes. */
  680. inode_lock(orphan_dir_inode);
  681. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  682. if (status < 0) {
  683. inode_unlock(orphan_dir_inode);
  684. mlog_errno(status);
  685. goto bail;
  686. }
  687. }
  688. /* we do this while holding the orphan dir lock because we
  689. * don't want recovery being run from another node to try an
  690. * inode delete underneath us -- this will result in two nodes
  691. * truncating the same file! */
  692. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  693. if (status < 0) {
  694. mlog_errno(status);
  695. goto bail_unlock_dir;
  696. }
  697. /* Remove any dir index tree */
  698. if (S_ISDIR(inode->i_mode)) {
  699. status = ocfs2_dx_dir_truncate(inode, di_bh);
  700. if (status) {
  701. mlog_errno(status);
  702. goto bail_unlock_dir;
  703. }
  704. }
  705. /*Free extended attribute resources associated with this inode.*/
  706. status = ocfs2_xattr_remove(inode, di_bh);
  707. if (status < 0) {
  708. mlog_errno(status);
  709. goto bail_unlock_dir;
  710. }
  711. status = ocfs2_remove_refcount_tree(inode, di_bh);
  712. if (status < 0) {
  713. mlog_errno(status);
  714. goto bail_unlock_dir;
  715. }
  716. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  717. orphan_dir_bh);
  718. if (status < 0)
  719. mlog_errno(status);
  720. bail_unlock_dir:
  721. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
  722. return status;
  723. ocfs2_inode_unlock(orphan_dir_inode, 1);
  724. inode_unlock(orphan_dir_inode);
  725. brelse(orphan_dir_bh);
  726. bail:
  727. iput(orphan_dir_inode);
  728. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  729. return status;
  730. }
  731. /* There is a series of simple checks that should be done before a
  732. * trylock is even considered. Encapsulate those in this function. */
  733. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  734. {
  735. int ret = 0;
  736. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  737. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  738. trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
  739. (unsigned long long)oi->ip_blkno,
  740. oi->ip_flags);
  741. /* We shouldn't be getting here for the root directory
  742. * inode.. */
  743. if (inode == osb->root_inode) {
  744. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  745. goto bail;
  746. }
  747. /*
  748. * If we're coming from downconvert_thread we can't go into our own
  749. * voting [hello, deadlock city!] so we cannot delete the inode. But
  750. * since we dropped last inode ref when downconverting dentry lock,
  751. * we cannot have the file open and thus the node doing unlink will
  752. * take care of deleting the inode.
  753. */
  754. if (current == osb->dc_task)
  755. goto bail;
  756. spin_lock(&oi->ip_lock);
  757. /* OCFS2 *never* deletes system files. This should technically
  758. * never get here as system file inodes should always have a
  759. * positive link count. */
  760. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  761. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  762. (unsigned long long)oi->ip_blkno);
  763. goto bail_unlock;
  764. }
  765. ret = 1;
  766. bail_unlock:
  767. spin_unlock(&oi->ip_lock);
  768. bail:
  769. return ret;
  770. }
  771. /* Query the cluster to determine whether we should wipe an inode from
  772. * disk or not.
  773. *
  774. * Requires the inode to have the cluster lock. */
  775. static int ocfs2_query_inode_wipe(struct inode *inode,
  776. struct buffer_head *di_bh,
  777. int *wipe)
  778. {
  779. int status = 0, reason = 0;
  780. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  781. struct ocfs2_dinode *di;
  782. *wipe = 0;
  783. trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
  784. inode->i_nlink);
  785. /* While we were waiting for the cluster lock in
  786. * ocfs2_delete_inode, another node might have asked to delete
  787. * the inode. Recheck our flags to catch this. */
  788. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  789. reason = 1;
  790. goto bail;
  791. }
  792. /* Now that we have an up to date inode, we can double check
  793. * the link count. */
  794. if (inode->i_nlink)
  795. goto bail;
  796. /* Do some basic inode verification... */
  797. di = (struct ocfs2_dinode *) di_bh->b_data;
  798. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
  799. !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  800. /*
  801. * Inodes in the orphan dir must have ORPHANED_FL. The only
  802. * inodes that come back out of the orphan dir are reflink
  803. * targets. A reflink target may be moved out of the orphan
  804. * dir between the time we scan the directory and the time we
  805. * process it. This would lead to HAS_REFCOUNT_FL being set but
  806. * ORPHANED_FL not.
  807. */
  808. if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
  809. reason = 2;
  810. goto bail;
  811. }
  812. /* for lack of a better error? */
  813. status = -EEXIST;
  814. mlog(ML_ERROR,
  815. "Inode %llu (on-disk %llu) not orphaned! "
  816. "Disk flags 0x%x, inode flags 0x%x\n",
  817. (unsigned long long)oi->ip_blkno,
  818. (unsigned long long)le64_to_cpu(di->i_blkno),
  819. le32_to_cpu(di->i_flags), oi->ip_flags);
  820. goto bail;
  821. }
  822. /* has someone already deleted us?! baaad... */
  823. if (di->i_dtime) {
  824. status = -EEXIST;
  825. mlog_errno(status);
  826. goto bail;
  827. }
  828. /*
  829. * This is how ocfs2 determines whether an inode is still live
  830. * within the cluster. Every node takes a shared read lock on
  831. * the inode open lock in ocfs2_read_locked_inode(). When we
  832. * get to ->delete_inode(), each node tries to convert it's
  833. * lock to an exclusive. Trylocks are serialized by the inode
  834. * meta data lock. If the upconvert succeeds, we know the inode
  835. * is no longer live and can be deleted.
  836. *
  837. * Though we call this with the meta data lock held, the
  838. * trylock keeps us from ABBA deadlock.
  839. */
  840. status = ocfs2_try_open_lock(inode, 1);
  841. if (status == -EAGAIN) {
  842. status = 0;
  843. reason = 3;
  844. goto bail;
  845. }
  846. if (status < 0) {
  847. mlog_errno(status);
  848. goto bail;
  849. }
  850. *wipe = 1;
  851. trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
  852. bail:
  853. trace_ocfs2_query_inode_wipe_end(status, reason);
  854. return status;
  855. }
  856. /* Support function for ocfs2_delete_inode. Will help us keep the
  857. * inode data in a consistent state for clear_inode. Always truncates
  858. * pages, optionally sync's them first. */
  859. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  860. int sync_data)
  861. {
  862. trace_ocfs2_cleanup_delete_inode(
  863. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  864. if (sync_data)
  865. filemap_write_and_wait(inode->i_mapping);
  866. truncate_inode_pages_final(&inode->i_data);
  867. }
  868. static void ocfs2_delete_inode(struct inode *inode)
  869. {
  870. int wipe, status;
  871. sigset_t oldset;
  872. struct buffer_head *di_bh = NULL;
  873. struct ocfs2_dinode *di = NULL;
  874. trace_ocfs2_delete_inode(inode->i_ino,
  875. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  876. is_bad_inode(inode));
  877. /* When we fail in read_inode() we mark inode as bad. The second test
  878. * catches the case when inode allocation fails before allocating
  879. * a block for inode. */
  880. if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
  881. goto bail;
  882. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  883. /* It's probably not necessary to truncate_inode_pages
  884. * here but we do it for safety anyway (it will most
  885. * likely be a no-op anyway) */
  886. ocfs2_cleanup_delete_inode(inode, 0);
  887. goto bail;
  888. }
  889. dquot_initialize(inode);
  890. /* We want to block signals in delete_inode as the lock and
  891. * messaging paths may return us -ERESTARTSYS. Which would
  892. * cause us to exit early, resulting in inodes being orphaned
  893. * forever. */
  894. ocfs2_block_signals(&oldset);
  895. /*
  896. * Synchronize us against ocfs2_get_dentry. We take this in
  897. * shared mode so that all nodes can still concurrently
  898. * process deletes.
  899. */
  900. status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
  901. if (status < 0) {
  902. mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
  903. ocfs2_cleanup_delete_inode(inode, 0);
  904. goto bail_unblock;
  905. }
  906. /* Lock down the inode. This gives us an up to date view of
  907. * it's metadata (for verification), and allows us to
  908. * serialize delete_inode on multiple nodes.
  909. *
  910. * Even though we might be doing a truncate, we don't take the
  911. * allocation lock here as it won't be needed - nobody will
  912. * have the file open.
  913. */
  914. status = ocfs2_inode_lock(inode, &di_bh, 1);
  915. if (status < 0) {
  916. if (status != -ENOENT)
  917. mlog_errno(status);
  918. ocfs2_cleanup_delete_inode(inode, 0);
  919. goto bail_unlock_nfs_sync;
  920. }
  921. di = (struct ocfs2_dinode *)di_bh->b_data;
  922. /* Skip inode deletion and wait for dio orphan entry recovered
  923. * first */
  924. if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
  925. ocfs2_cleanup_delete_inode(inode, 0);
  926. goto bail_unlock_inode;
  927. }
  928. /* Query the cluster. This will be the final decision made
  929. * before we go ahead and wipe the inode. */
  930. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  931. if (!wipe || status < 0) {
  932. /* Error and remote inode busy both mean we won't be
  933. * removing the inode, so they take almost the same
  934. * path. */
  935. if (status < 0)
  936. mlog_errno(status);
  937. /* Someone in the cluster has disallowed a wipe of
  938. * this inode, or it was never completely
  939. * orphaned. Write out the pages and exit now. */
  940. ocfs2_cleanup_delete_inode(inode, 1);
  941. goto bail_unlock_inode;
  942. }
  943. ocfs2_cleanup_delete_inode(inode, 0);
  944. status = ocfs2_wipe_inode(inode, di_bh);
  945. if (status < 0) {
  946. if (status != -EDEADLK)
  947. mlog_errno(status);
  948. goto bail_unlock_inode;
  949. }
  950. /*
  951. * Mark the inode as successfully deleted.
  952. *
  953. * This is important for ocfs2_clear_inode() as it will check
  954. * this flag and skip any checkpointing work
  955. *
  956. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  957. * the LVB for other nodes.
  958. */
  959. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  960. bail_unlock_inode:
  961. ocfs2_inode_unlock(inode, 1);
  962. brelse(di_bh);
  963. bail_unlock_nfs_sync:
  964. ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
  965. bail_unblock:
  966. ocfs2_unblock_signals(&oldset);
  967. bail:
  968. return;
  969. }
  970. static void ocfs2_clear_inode(struct inode *inode)
  971. {
  972. int status;
  973. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  974. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  975. clear_inode(inode);
  976. trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
  977. inode->i_nlink);
  978. mlog_bug_on_msg(osb == NULL,
  979. "Inode=%lu\n", inode->i_ino);
  980. dquot_drop(inode);
  981. /* To preven remote deletes we hold open lock before, now it
  982. * is time to unlock PR and EX open locks. */
  983. ocfs2_open_unlock(inode);
  984. /* Do these before all the other work so that we don't bounce
  985. * the downconvert thread while waiting to destroy the locks. */
  986. ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres);
  987. ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres);
  988. ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres);
  989. ocfs2_resv_discard(&osb->osb_la_resmap,
  990. &oi->ip_la_data_resv);
  991. ocfs2_resv_init_once(&oi->ip_la_data_resv);
  992. /* We very well may get a clear_inode before all an inodes
  993. * metadata has hit disk. Of course, we can't drop any cluster
  994. * locks until the journal has finished with it. The only
  995. * exception here are successfully wiped inodes - their
  996. * metadata can now be considered to be part of the system
  997. * inodes from which it came. */
  998. if (!(oi->ip_flags & OCFS2_INODE_DELETED))
  999. ocfs2_checkpoint_inode(inode);
  1000. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  1001. "Clear inode of %llu, inode has io markers\n",
  1002. (unsigned long long)oi->ip_blkno);
  1003. mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list),
  1004. "Clear inode of %llu, inode has unwritten extents\n",
  1005. (unsigned long long)oi->ip_blkno);
  1006. ocfs2_extent_map_trunc(inode, 0);
  1007. status = ocfs2_drop_inode_locks(inode);
  1008. if (status < 0)
  1009. mlog_errno(status);
  1010. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  1011. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  1012. ocfs2_lock_res_free(&oi->ip_open_lockres);
  1013. ocfs2_metadata_cache_exit(INODE_CACHE(inode));
  1014. mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
  1015. "Clear inode of %llu, inode has %u cache items\n",
  1016. (unsigned long long)oi->ip_blkno,
  1017. INODE_CACHE(inode)->ci_num_cached);
  1018. mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
  1019. "Clear inode of %llu, inode has a bad flag\n",
  1020. (unsigned long long)oi->ip_blkno);
  1021. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  1022. "Clear inode of %llu, inode is locked\n",
  1023. (unsigned long long)oi->ip_blkno);
  1024. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  1025. "Clear inode of %llu, io_mutex is locked\n",
  1026. (unsigned long long)oi->ip_blkno);
  1027. mutex_unlock(&oi->ip_io_mutex);
  1028. /*
  1029. * down_trylock() returns 0, down_write_trylock() returns 1
  1030. * kernel 1, world 0
  1031. */
  1032. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  1033. "Clear inode of %llu, alloc_sem is locked\n",
  1034. (unsigned long long)oi->ip_blkno);
  1035. up_write(&oi->ip_alloc_sem);
  1036. mlog_bug_on_msg(oi->ip_open_count,
  1037. "Clear inode of %llu has open count %d\n",
  1038. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  1039. /* Clear all other flags. */
  1040. oi->ip_flags = 0;
  1041. oi->ip_dir_start_lookup = 0;
  1042. oi->ip_blkno = 0ULL;
  1043. /*
  1044. * ip_jinode is used to track txns against this inode. We ensure that
  1045. * the journal is flushed before journal shutdown. Thus it is safe to
  1046. * have inodes get cleaned up after journal shutdown.
  1047. */
  1048. jbd2_journal_release_jbd_inode(osb->journal->j_journal,
  1049. &oi->ip_jinode);
  1050. }
  1051. void ocfs2_evict_inode(struct inode *inode)
  1052. {
  1053. if (!inode->i_nlink ||
  1054. (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
  1055. ocfs2_delete_inode(inode);
  1056. } else {
  1057. truncate_inode_pages_final(&inode->i_data);
  1058. }
  1059. ocfs2_clear_inode(inode);
  1060. }
  1061. /* Called under inode_lock, with no more references on the
  1062. * struct inode, so it's safe here to check the flags field
  1063. * and to manipulate i_nlink without any other locks. */
  1064. int ocfs2_drop_inode(struct inode *inode)
  1065. {
  1066. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1067. trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
  1068. inode->i_nlink, oi->ip_flags);
  1069. assert_spin_locked(&inode->i_lock);
  1070. inode->i_state |= I_WILL_FREE;
  1071. spin_unlock(&inode->i_lock);
  1072. write_inode_now(inode, 1);
  1073. spin_lock(&inode->i_lock);
  1074. WARN_ON(inode->i_state & I_NEW);
  1075. inode->i_state &= ~I_WILL_FREE;
  1076. return 1;
  1077. }
  1078. /*
  1079. * This is called from our getattr.
  1080. */
  1081. int ocfs2_inode_revalidate(struct dentry *dentry)
  1082. {
  1083. struct inode *inode = d_inode(dentry);
  1084. int status = 0;
  1085. trace_ocfs2_inode_revalidate(inode,
  1086. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
  1087. inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
  1088. if (!inode) {
  1089. status = -ENOENT;
  1090. goto bail;
  1091. }
  1092. spin_lock(&OCFS2_I(inode)->ip_lock);
  1093. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  1094. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1095. status = -ENOENT;
  1096. goto bail;
  1097. }
  1098. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1099. /* Let ocfs2_inode_lock do the work of updating our struct
  1100. * inode for us. */
  1101. status = ocfs2_inode_lock(inode, NULL, 0);
  1102. if (status < 0) {
  1103. if (status != -ENOENT)
  1104. mlog_errno(status);
  1105. goto bail;
  1106. }
  1107. ocfs2_inode_unlock(inode, 0);
  1108. bail:
  1109. return status;
  1110. }
  1111. /*
  1112. * Updates a disk inode from a
  1113. * struct inode.
  1114. * Only takes ip_lock.
  1115. */
  1116. int ocfs2_mark_inode_dirty(handle_t *handle,
  1117. struct inode *inode,
  1118. struct buffer_head *bh)
  1119. {
  1120. int status;
  1121. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1122. trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
  1123. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1124. OCFS2_JOURNAL_ACCESS_WRITE);
  1125. if (status < 0) {
  1126. mlog_errno(status);
  1127. goto leave;
  1128. }
  1129. spin_lock(&OCFS2_I(inode)->ip_lock);
  1130. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1131. ocfs2_get_inode_flags(OCFS2_I(inode));
  1132. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1133. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1134. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1135. fe->i_size = cpu_to_le64(i_size_read(inode));
  1136. ocfs2_set_links_count(fe, inode->i_nlink);
  1137. fe->i_uid = cpu_to_le32(i_uid_read(inode));
  1138. fe->i_gid = cpu_to_le32(i_gid_read(inode));
  1139. fe->i_mode = cpu_to_le16(inode->i_mode);
  1140. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1141. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1142. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1143. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1144. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1145. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1146. ocfs2_journal_dirty(handle, bh);
  1147. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1148. leave:
  1149. return status;
  1150. }
  1151. /*
  1152. *
  1153. * Updates a struct inode from a disk inode.
  1154. * does no i/o, only takes ip_lock.
  1155. */
  1156. void ocfs2_refresh_inode(struct inode *inode,
  1157. struct ocfs2_dinode *fe)
  1158. {
  1159. spin_lock(&OCFS2_I(inode)->ip_lock);
  1160. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1161. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1162. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1163. ocfs2_set_inode_flags(inode);
  1164. i_size_write(inode, le64_to_cpu(fe->i_size));
  1165. set_nlink(inode, ocfs2_read_links_count(fe));
  1166. i_uid_write(inode, le32_to_cpu(fe->i_uid));
  1167. i_gid_write(inode, le32_to_cpu(fe->i_gid));
  1168. inode->i_mode = le16_to_cpu(fe->i_mode);
  1169. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1170. inode->i_blocks = 0;
  1171. else
  1172. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1173. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1174. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1175. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1176. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1177. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1178. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1179. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1180. }
  1181. int ocfs2_validate_inode_block(struct super_block *sb,
  1182. struct buffer_head *bh)
  1183. {
  1184. int rc;
  1185. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1186. trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
  1187. BUG_ON(!buffer_uptodate(bh));
  1188. /*
  1189. * If the ecc fails, we return the error but otherwise
  1190. * leave the filesystem running. We know any error is
  1191. * local to this block.
  1192. */
  1193. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1194. if (rc) {
  1195. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  1196. (unsigned long long)bh->b_blocknr);
  1197. goto bail;
  1198. }
  1199. /*
  1200. * Errors after here are fatal.
  1201. */
  1202. rc = -EINVAL;
  1203. if (!OCFS2_IS_VALID_DINODE(di)) {
  1204. rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
  1205. (unsigned long long)bh->b_blocknr, 7,
  1206. di->i_signature);
  1207. goto bail;
  1208. }
  1209. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1210. rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
  1211. (unsigned long long)bh->b_blocknr,
  1212. (unsigned long long)le64_to_cpu(di->i_blkno));
  1213. goto bail;
  1214. }
  1215. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1216. rc = ocfs2_error(sb,
  1217. "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
  1218. (unsigned long long)bh->b_blocknr);
  1219. goto bail;
  1220. }
  1221. if (le32_to_cpu(di->i_fs_generation) !=
  1222. OCFS2_SB(sb)->fs_generation) {
  1223. rc = ocfs2_error(sb,
  1224. "Invalid dinode #%llu: fs_generation is %u\n",
  1225. (unsigned long long)bh->b_blocknr,
  1226. le32_to_cpu(di->i_fs_generation));
  1227. goto bail;
  1228. }
  1229. rc = 0;
  1230. bail:
  1231. return rc;
  1232. }
  1233. static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
  1234. struct buffer_head *bh)
  1235. {
  1236. int rc = 0;
  1237. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1238. trace_ocfs2_filecheck_validate_inode_block(
  1239. (unsigned long long)bh->b_blocknr);
  1240. BUG_ON(!buffer_uptodate(bh));
  1241. /*
  1242. * Call ocfs2_validate_meta_ecc() first since it has ecc repair
  1243. * function, but we should not return error immediately when ecc
  1244. * validation fails, because the reason is quite likely the invalid
  1245. * inode number inputed.
  1246. */
  1247. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1248. if (rc) {
  1249. mlog(ML_ERROR,
  1250. "Filecheck: checksum failed for dinode %llu\n",
  1251. (unsigned long long)bh->b_blocknr);
  1252. rc = -OCFS2_FILECHECK_ERR_BLOCKECC;
  1253. }
  1254. if (!OCFS2_IS_VALID_DINODE(di)) {
  1255. mlog(ML_ERROR,
  1256. "Filecheck: invalid dinode #%llu: signature = %.*s\n",
  1257. (unsigned long long)bh->b_blocknr, 7, di->i_signature);
  1258. rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
  1259. goto bail;
  1260. } else if (rc)
  1261. goto bail;
  1262. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1263. mlog(ML_ERROR,
  1264. "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
  1265. (unsigned long long)bh->b_blocknr,
  1266. (unsigned long long)le64_to_cpu(di->i_blkno));
  1267. rc = -OCFS2_FILECHECK_ERR_BLOCKNO;
  1268. goto bail;
  1269. }
  1270. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1271. mlog(ML_ERROR,
  1272. "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
  1273. "not set\n",
  1274. (unsigned long long)bh->b_blocknr);
  1275. rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1276. goto bail;
  1277. }
  1278. if (le32_to_cpu(di->i_fs_generation) !=
  1279. OCFS2_SB(sb)->fs_generation) {
  1280. mlog(ML_ERROR,
  1281. "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
  1282. (unsigned long long)bh->b_blocknr,
  1283. le32_to_cpu(di->i_fs_generation));
  1284. rc = -OCFS2_FILECHECK_ERR_GENERATION;
  1285. }
  1286. bail:
  1287. return rc;
  1288. }
  1289. static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
  1290. struct buffer_head *bh)
  1291. {
  1292. int changed = 0;
  1293. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1294. if (!ocfs2_filecheck_validate_inode_block(sb, bh))
  1295. return 0;
  1296. trace_ocfs2_filecheck_repair_inode_block(
  1297. (unsigned long long)bh->b_blocknr);
  1298. if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) ||
  1299. ocfs2_is_soft_readonly(OCFS2_SB(sb))) {
  1300. mlog(ML_ERROR,
  1301. "Filecheck: cannot repair dinode #%llu "
  1302. "on readonly filesystem\n",
  1303. (unsigned long long)bh->b_blocknr);
  1304. return -OCFS2_FILECHECK_ERR_READONLY;
  1305. }
  1306. if (buffer_jbd(bh)) {
  1307. mlog(ML_ERROR,
  1308. "Filecheck: cannot repair dinode #%llu, "
  1309. "its buffer is in jbd\n",
  1310. (unsigned long long)bh->b_blocknr);
  1311. return -OCFS2_FILECHECK_ERR_INJBD;
  1312. }
  1313. if (!OCFS2_IS_VALID_DINODE(di)) {
  1314. /* Cannot fix invalid inode block */
  1315. return -OCFS2_FILECHECK_ERR_INVALIDINO;
  1316. }
  1317. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1318. /* Cannot just add VALID_FL flag back as a fix,
  1319. * need more things to check here.
  1320. */
  1321. return -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1322. }
  1323. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1324. di->i_blkno = cpu_to_le64(bh->b_blocknr);
  1325. changed = 1;
  1326. mlog(ML_ERROR,
  1327. "Filecheck: reset dinode #%llu: i_blkno to %llu\n",
  1328. (unsigned long long)bh->b_blocknr,
  1329. (unsigned long long)le64_to_cpu(di->i_blkno));
  1330. }
  1331. if (le32_to_cpu(di->i_fs_generation) !=
  1332. OCFS2_SB(sb)->fs_generation) {
  1333. di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  1334. changed = 1;
  1335. mlog(ML_ERROR,
  1336. "Filecheck: reset dinode #%llu: fs_generation to %u\n",
  1337. (unsigned long long)bh->b_blocknr,
  1338. le32_to_cpu(di->i_fs_generation));
  1339. }
  1340. if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
  1341. ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
  1342. mark_buffer_dirty(bh);
  1343. mlog(ML_ERROR,
  1344. "Filecheck: reset dinode #%llu: compute meta ecc\n",
  1345. (unsigned long long)bh->b_blocknr);
  1346. }
  1347. return 0;
  1348. }
  1349. static int
  1350. ocfs2_filecheck_read_inode_block_full(struct inode *inode,
  1351. struct buffer_head **bh,
  1352. int flags, int type)
  1353. {
  1354. int rc;
  1355. struct buffer_head *tmp = *bh;
  1356. if (!type) /* Check inode block */
  1357. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1358. OCFS2_I(inode)->ip_blkno,
  1359. 1, &tmp, flags,
  1360. ocfs2_filecheck_validate_inode_block);
  1361. else /* Repair inode block */
  1362. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1363. OCFS2_I(inode)->ip_blkno,
  1364. 1, &tmp, flags,
  1365. ocfs2_filecheck_repair_inode_block);
  1366. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1367. if (!rc && !*bh)
  1368. *bh = tmp;
  1369. return rc;
  1370. }
  1371. int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  1372. int flags)
  1373. {
  1374. int rc;
  1375. struct buffer_head *tmp = *bh;
  1376. rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
  1377. 1, &tmp, flags, ocfs2_validate_inode_block);
  1378. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1379. if (!rc && !*bh)
  1380. *bh = tmp;
  1381. return rc;
  1382. }
  1383. int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  1384. {
  1385. return ocfs2_read_inode_block_full(inode, bh, 0);
  1386. }
  1387. static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
  1388. {
  1389. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1390. return oi->ip_blkno;
  1391. }
  1392. static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
  1393. {
  1394. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1395. return oi->vfs_inode.i_sb;
  1396. }
  1397. static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
  1398. {
  1399. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1400. spin_lock(&oi->ip_lock);
  1401. }
  1402. static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
  1403. {
  1404. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1405. spin_unlock(&oi->ip_lock);
  1406. }
  1407. static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
  1408. {
  1409. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1410. mutex_lock(&oi->ip_io_mutex);
  1411. }
  1412. static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
  1413. {
  1414. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1415. mutex_unlock(&oi->ip_io_mutex);
  1416. }
  1417. const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
  1418. .co_owner = ocfs2_inode_cache_owner,
  1419. .co_get_super = ocfs2_inode_cache_get_super,
  1420. .co_cache_lock = ocfs2_inode_cache_lock,
  1421. .co_cache_unlock = ocfs2_inode_cache_unlock,
  1422. .co_io_lock = ocfs2_inode_cache_io_lock,
  1423. .co_io_unlock = ocfs2_inode_cache_io_unlock,
  1424. };