inode.c 40 KB

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