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