namei.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * namei.c
  5. *
  6. * Create and rename file, directory, symlinks
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * Portions of this code from linux/fs/ext3/dir.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/dir.c
  20. *
  21. * Copyright (C) 1991, 1992 Linux Torvalds
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public
  34. * License along with this program; if not, write to the
  35. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  36. * Boston, MA 021110-1307, USA.
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #include <linux/quotaops.h>
  43. #define MLOG_MASK_PREFIX ML_NAMEI
  44. #include <cluster/masklog.h>
  45. #include "ocfs2.h"
  46. #include "alloc.h"
  47. #include "dcache.h"
  48. #include "dir.h"
  49. #include "dlmglue.h"
  50. #include "extent_map.h"
  51. #include "file.h"
  52. #include "inode.h"
  53. #include "journal.h"
  54. #include "namei.h"
  55. #include "suballoc.h"
  56. #include "super.h"
  57. #include "symlink.h"
  58. #include "sysfile.h"
  59. #include "uptodate.h"
  60. #include "xattr.h"
  61. #include "acl.h"
  62. #include "buffer_head_io.h"
  63. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  64. struct inode *dir,
  65. struct inode *inode,
  66. struct dentry *dentry,
  67. dev_t dev,
  68. struct buffer_head **new_fe_bh,
  69. struct buffer_head *parent_fe_bh,
  70. handle_t *handle,
  71. struct ocfs2_alloc_context *inode_ac);
  72. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  73. struct inode **ret_orphan_dir,
  74. struct inode *inode,
  75. char *name,
  76. struct ocfs2_dir_lookup_result *lookup);
  77. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  78. handle_t *handle,
  79. struct inode *inode,
  80. struct ocfs2_dinode *fe,
  81. char *name,
  82. struct ocfs2_dir_lookup_result *lookup,
  83. struct inode *orphan_dir_inode);
  84. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  85. handle_t *handle,
  86. struct inode *inode,
  87. const char *symname);
  88. /* An orphan dir name is an 8 byte value, printed as a hex string */
  89. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  90. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  91. struct nameidata *nd)
  92. {
  93. int status;
  94. u64 blkno;
  95. struct inode *inode = NULL;
  96. struct dentry *ret;
  97. struct ocfs2_inode_info *oi;
  98. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  99. dentry->d_name.len, dentry->d_name.name);
  100. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  101. ret = ERR_PTR(-ENAMETOOLONG);
  102. goto bail;
  103. }
  104. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  105. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  106. status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
  107. if (status < 0) {
  108. if (status != -ENOENT)
  109. mlog_errno(status);
  110. ret = ERR_PTR(status);
  111. goto bail;
  112. }
  113. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  114. dentry->d_name.len, &blkno);
  115. if (status < 0)
  116. goto bail_add;
  117. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  118. if (IS_ERR(inode)) {
  119. ret = ERR_PTR(-EACCES);
  120. goto bail_unlock;
  121. }
  122. oi = OCFS2_I(inode);
  123. /* Clear any orphaned state... If we were able to look up the
  124. * inode from a directory, it certainly can't be orphaned. We
  125. * might have the bad state from a node which intended to
  126. * orphan this inode but crashed before it could commit the
  127. * unlink. */
  128. spin_lock(&oi->ip_lock);
  129. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  130. spin_unlock(&oi->ip_lock);
  131. bail_add:
  132. dentry->d_op = &ocfs2_dentry_ops;
  133. ret = d_splice_alias(inode, dentry);
  134. if (inode) {
  135. /*
  136. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  137. * dentry, it will d_move() it on top of ourse. The
  138. * return value will indicate this however, so in
  139. * those cases, we switch them around for the locking
  140. * code.
  141. *
  142. * NOTE: This dentry already has ->d_op set from
  143. * ocfs2_get_parent() and ocfs2_get_dentry()
  144. */
  145. if (ret)
  146. dentry = ret;
  147. status = ocfs2_dentry_attach_lock(dentry, inode,
  148. OCFS2_I(dir)->ip_blkno);
  149. if (status) {
  150. mlog_errno(status);
  151. ret = ERR_PTR(status);
  152. goto bail_unlock;
  153. }
  154. }
  155. bail_unlock:
  156. /* Don't drop the cluster lock until *after* the d_add --
  157. * unlink on another node will message us to remove that
  158. * dentry under this lock so otherwise we can race this with
  159. * the downconvert thread and have a stale dentry. */
  160. ocfs2_inode_unlock(dir, 0);
  161. bail:
  162. mlog_exit_ptr(ret);
  163. return ret;
  164. }
  165. static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
  166. {
  167. struct inode *inode;
  168. inode = new_inode(dir->i_sb);
  169. if (!inode) {
  170. mlog(ML_ERROR, "new_inode failed!\n");
  171. return NULL;
  172. }
  173. /* populate as many fields early on as possible - many of
  174. * these are used by the support functions here and in
  175. * callers. */
  176. if (S_ISDIR(mode))
  177. inode->i_nlink = 2;
  178. else
  179. inode->i_nlink = 1;
  180. inode->i_uid = current_fsuid();
  181. if (dir->i_mode & S_ISGID) {
  182. inode->i_gid = dir->i_gid;
  183. if (S_ISDIR(mode))
  184. mode |= S_ISGID;
  185. } else
  186. inode->i_gid = current_fsgid();
  187. inode->i_mode = mode;
  188. vfs_dq_init(inode);
  189. return inode;
  190. }
  191. static int ocfs2_mknod(struct inode *dir,
  192. struct dentry *dentry,
  193. int mode,
  194. dev_t dev)
  195. {
  196. int status = 0;
  197. struct buffer_head *parent_fe_bh = NULL;
  198. handle_t *handle = NULL;
  199. struct ocfs2_super *osb;
  200. struct ocfs2_dinode *dirfe;
  201. struct buffer_head *new_fe_bh = NULL;
  202. struct inode *inode = NULL;
  203. struct ocfs2_alloc_context *inode_ac = NULL;
  204. struct ocfs2_alloc_context *data_ac = NULL;
  205. struct ocfs2_alloc_context *meta_ac = NULL;
  206. int want_clusters = 0;
  207. int want_meta = 0;
  208. int xattr_credits = 0;
  209. struct ocfs2_security_xattr_info si = {
  210. .enable = 1,
  211. };
  212. int did_quota_inode = 0;
  213. struct ocfs2_dir_lookup_result lookup = { NULL, };
  214. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
  215. (unsigned long)dev, dentry->d_name.len,
  216. dentry->d_name.name);
  217. /* get our super block */
  218. osb = OCFS2_SB(dir->i_sb);
  219. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  220. if (status < 0) {
  221. if (status != -ENOENT)
  222. mlog_errno(status);
  223. return status;
  224. }
  225. if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
  226. status = -EMLINK;
  227. goto leave;
  228. }
  229. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  230. if (!ocfs2_read_links_count(dirfe)) {
  231. /* can't make a file in a deleted directory. */
  232. status = -ENOENT;
  233. goto leave;
  234. }
  235. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  236. dentry->d_name.len);
  237. if (status)
  238. goto leave;
  239. /* get a spot inside the dir. */
  240. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  241. dentry->d_name.name,
  242. dentry->d_name.len, &lookup);
  243. if (status < 0) {
  244. mlog_errno(status);
  245. goto leave;
  246. }
  247. /* reserve an inode spot */
  248. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  249. if (status < 0) {
  250. if (status != -ENOSPC)
  251. mlog_errno(status);
  252. goto leave;
  253. }
  254. inode = ocfs2_get_init_inode(dir, mode);
  255. if (!inode) {
  256. status = -ENOMEM;
  257. mlog_errno(status);
  258. goto leave;
  259. }
  260. /* get security xattr */
  261. status = ocfs2_init_security_get(inode, dir, &si);
  262. if (status) {
  263. if (status == -EOPNOTSUPP)
  264. si.enable = 0;
  265. else {
  266. mlog_errno(status);
  267. goto leave;
  268. }
  269. }
  270. /* calculate meta data/clusters for setting security and acl xattr */
  271. status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
  272. &si, &want_clusters,
  273. &xattr_credits, &want_meta);
  274. if (status < 0) {
  275. mlog_errno(status);
  276. goto leave;
  277. }
  278. /* Reserve a cluster if creating an extent based directory. */
  279. if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
  280. want_clusters += 1;
  281. /* Dir indexing requires extra space as well */
  282. if (ocfs2_supports_indexed_dirs(osb))
  283. want_meta++;
  284. }
  285. status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
  286. if (status < 0) {
  287. if (status != -ENOSPC)
  288. mlog_errno(status);
  289. goto leave;
  290. }
  291. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  292. if (status < 0) {
  293. if (status != -ENOSPC)
  294. mlog_errno(status);
  295. goto leave;
  296. }
  297. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
  298. S_ISDIR(mode),
  299. xattr_credits));
  300. if (IS_ERR(handle)) {
  301. status = PTR_ERR(handle);
  302. handle = NULL;
  303. mlog_errno(status);
  304. goto leave;
  305. }
  306. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  307. * to be called. */
  308. if (sb_any_quota_active(osb->sb) &&
  309. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  310. status = -EDQUOT;
  311. goto leave;
  312. }
  313. did_quota_inode = 1;
  314. /* do the real work now. */
  315. status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
  316. &new_fe_bh, parent_fe_bh, handle,
  317. inode_ac);
  318. if (status < 0) {
  319. mlog_errno(status);
  320. goto leave;
  321. }
  322. if (S_ISDIR(mode)) {
  323. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  324. new_fe_bh, data_ac, meta_ac);
  325. if (status < 0) {
  326. mlog_errno(status);
  327. goto leave;
  328. }
  329. status = ocfs2_journal_access_di(handle, dir, parent_fe_bh,
  330. OCFS2_JOURNAL_ACCESS_WRITE);
  331. if (status < 0) {
  332. mlog_errno(status);
  333. goto leave;
  334. }
  335. ocfs2_add_links_count(dirfe, 1);
  336. status = ocfs2_journal_dirty(handle, parent_fe_bh);
  337. if (status < 0) {
  338. mlog_errno(status);
  339. goto leave;
  340. }
  341. inc_nlink(dir);
  342. }
  343. status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
  344. meta_ac, data_ac);
  345. if (status < 0) {
  346. mlog_errno(status);
  347. goto leave;
  348. }
  349. if (si.enable) {
  350. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  351. meta_ac, data_ac);
  352. if (status < 0) {
  353. mlog_errno(status);
  354. goto leave;
  355. }
  356. }
  357. status = ocfs2_add_entry(handle, dentry, inode,
  358. OCFS2_I(inode)->ip_blkno, parent_fe_bh,
  359. &lookup);
  360. if (status < 0) {
  361. mlog_errno(status);
  362. goto leave;
  363. }
  364. status = ocfs2_dentry_attach_lock(dentry, inode,
  365. OCFS2_I(dir)->ip_blkno);
  366. if (status) {
  367. mlog_errno(status);
  368. goto leave;
  369. }
  370. insert_inode_hash(inode);
  371. dentry->d_op = &ocfs2_dentry_ops;
  372. d_instantiate(dentry, inode);
  373. status = 0;
  374. leave:
  375. if (status < 0 && did_quota_inode)
  376. vfs_dq_free_inode(inode);
  377. if (handle)
  378. ocfs2_commit_trans(osb, handle);
  379. ocfs2_inode_unlock(dir, 1);
  380. if (status == -ENOSPC)
  381. mlog(0, "Disk is full\n");
  382. brelse(new_fe_bh);
  383. brelse(parent_fe_bh);
  384. kfree(si.name);
  385. kfree(si.value);
  386. ocfs2_free_dir_lookup_result(&lookup);
  387. if ((status < 0) && inode) {
  388. clear_nlink(inode);
  389. iput(inode);
  390. }
  391. if (inode_ac)
  392. ocfs2_free_alloc_context(inode_ac);
  393. if (data_ac)
  394. ocfs2_free_alloc_context(data_ac);
  395. if (meta_ac)
  396. ocfs2_free_alloc_context(meta_ac);
  397. mlog_exit(status);
  398. return status;
  399. }
  400. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  401. struct inode *dir,
  402. struct inode *inode,
  403. struct dentry *dentry,
  404. dev_t dev,
  405. struct buffer_head **new_fe_bh,
  406. struct buffer_head *parent_fe_bh,
  407. handle_t *handle,
  408. struct ocfs2_alloc_context *inode_ac)
  409. {
  410. int status = 0;
  411. struct ocfs2_dinode *fe = NULL;
  412. struct ocfs2_extent_list *fel;
  413. u64 fe_blkno = 0;
  414. u16 suballoc_bit;
  415. u16 feat;
  416. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
  417. inode->i_mode, (unsigned long)dev, dentry->d_name.len,
  418. dentry->d_name.name);
  419. *new_fe_bh = NULL;
  420. status = ocfs2_claim_new_inode(osb, handle, dir, parent_fe_bh,
  421. inode_ac, &suballoc_bit, &fe_blkno);
  422. if (status < 0) {
  423. mlog_errno(status);
  424. goto leave;
  425. }
  426. /* populate as many fields early on as possible - many of
  427. * these are used by the support functions here and in
  428. * callers. */
  429. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  430. OCFS2_I(inode)->ip_blkno = fe_blkno;
  431. spin_lock(&osb->osb_lock);
  432. inode->i_generation = osb->s_next_generation++;
  433. spin_unlock(&osb->osb_lock);
  434. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  435. if (!*new_fe_bh) {
  436. status = -EIO;
  437. mlog_errno(status);
  438. goto leave;
  439. }
  440. ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
  441. status = ocfs2_journal_access_di(handle, inode, *new_fe_bh,
  442. OCFS2_JOURNAL_ACCESS_CREATE);
  443. if (status < 0) {
  444. mlog_errno(status);
  445. goto leave;
  446. }
  447. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  448. memset(fe, 0, osb->sb->s_blocksize);
  449. fe->i_generation = cpu_to_le32(inode->i_generation);
  450. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  451. fe->i_blkno = cpu_to_le64(fe_blkno);
  452. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  453. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  454. fe->i_uid = cpu_to_le32(inode->i_uid);
  455. fe->i_gid = cpu_to_le32(inode->i_gid);
  456. fe->i_mode = cpu_to_le16(inode->i_mode);
  457. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  458. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  459. ocfs2_set_links_count(fe, inode->i_nlink);
  460. fe->i_last_eb_blk = 0;
  461. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  462. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  463. fe->i_atime = fe->i_ctime = fe->i_mtime =
  464. cpu_to_le64(CURRENT_TIME.tv_sec);
  465. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  466. cpu_to_le32(CURRENT_TIME.tv_nsec);
  467. fe->i_dtime = 0;
  468. /*
  469. * If supported, directories start with inline data. If inline
  470. * isn't supported, but indexing is, we start them as indexed.
  471. */
  472. feat = le16_to_cpu(fe->i_dyn_features);
  473. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  474. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  475. fe->id2.i_data.id_count = cpu_to_le16(
  476. ocfs2_max_inline_data_with_xattr(osb->sb, fe));
  477. } else {
  478. fel = &fe->id2.i_list;
  479. fel->l_tree_depth = 0;
  480. fel->l_next_free_rec = 0;
  481. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  482. }
  483. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  484. if (status < 0) {
  485. mlog_errno(status);
  486. goto leave;
  487. }
  488. ocfs2_populate_inode(inode, fe, 1);
  489. ocfs2_inode_set_new(osb, inode);
  490. if (!ocfs2_mount_local(osb)) {
  491. status = ocfs2_create_new_inode_locks(inode);
  492. if (status < 0)
  493. mlog_errno(status);
  494. }
  495. status = 0; /* error in ocfs2_create_new_inode_locks is not
  496. * critical */
  497. leave:
  498. if (status < 0) {
  499. if (*new_fe_bh) {
  500. brelse(*new_fe_bh);
  501. *new_fe_bh = NULL;
  502. }
  503. }
  504. mlog_exit(status);
  505. return status;
  506. }
  507. static int ocfs2_mkdir(struct inode *dir,
  508. struct dentry *dentry,
  509. int mode)
  510. {
  511. int ret;
  512. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  513. dentry->d_name.len, dentry->d_name.name);
  514. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  515. mlog_exit(ret);
  516. return ret;
  517. }
  518. static int ocfs2_create(struct inode *dir,
  519. struct dentry *dentry,
  520. int mode,
  521. struct nameidata *nd)
  522. {
  523. int ret;
  524. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  525. dentry->d_name.len, dentry->d_name.name);
  526. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  527. mlog_exit(ret);
  528. return ret;
  529. }
  530. static int ocfs2_link(struct dentry *old_dentry,
  531. struct inode *dir,
  532. struct dentry *dentry)
  533. {
  534. handle_t *handle;
  535. struct inode *inode = old_dentry->d_inode;
  536. int err;
  537. struct buffer_head *fe_bh = NULL;
  538. struct buffer_head *parent_fe_bh = NULL;
  539. struct ocfs2_dinode *fe = NULL;
  540. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  541. struct ocfs2_dir_lookup_result lookup = { NULL, };
  542. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  543. old_dentry->d_name.len, old_dentry->d_name.name,
  544. dentry->d_name.len, dentry->d_name.name);
  545. if (S_ISDIR(inode->i_mode))
  546. return -EPERM;
  547. err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
  548. if (err < 0) {
  549. if (err != -ENOENT)
  550. mlog_errno(err);
  551. return err;
  552. }
  553. if (!dir->i_nlink) {
  554. err = -ENOENT;
  555. goto out;
  556. }
  557. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  558. dentry->d_name.len);
  559. if (err)
  560. goto out;
  561. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  562. dentry->d_name.name,
  563. dentry->d_name.len, &lookup);
  564. if (err < 0) {
  565. mlog_errno(err);
  566. goto out;
  567. }
  568. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  569. if (err < 0) {
  570. if (err != -ENOENT)
  571. mlog_errno(err);
  572. goto out;
  573. }
  574. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  575. if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
  576. err = -EMLINK;
  577. goto out_unlock_inode;
  578. }
  579. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  580. if (IS_ERR(handle)) {
  581. err = PTR_ERR(handle);
  582. handle = NULL;
  583. mlog_errno(err);
  584. goto out_unlock_inode;
  585. }
  586. err = ocfs2_journal_access_di(handle, inode, fe_bh,
  587. OCFS2_JOURNAL_ACCESS_WRITE);
  588. if (err < 0) {
  589. mlog_errno(err);
  590. goto out_commit;
  591. }
  592. inc_nlink(inode);
  593. inode->i_ctime = CURRENT_TIME;
  594. ocfs2_set_links_count(fe, inode->i_nlink);
  595. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  596. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  597. err = ocfs2_journal_dirty(handle, fe_bh);
  598. if (err < 0) {
  599. ocfs2_add_links_count(fe, -1);
  600. drop_nlink(inode);
  601. mlog_errno(err);
  602. goto out_commit;
  603. }
  604. err = ocfs2_add_entry(handle, dentry, inode,
  605. OCFS2_I(inode)->ip_blkno,
  606. parent_fe_bh, &lookup);
  607. if (err) {
  608. ocfs2_add_links_count(fe, -1);
  609. drop_nlink(inode);
  610. mlog_errno(err);
  611. goto out_commit;
  612. }
  613. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  614. if (err) {
  615. mlog_errno(err);
  616. goto out_commit;
  617. }
  618. atomic_inc(&inode->i_count);
  619. dentry->d_op = &ocfs2_dentry_ops;
  620. d_instantiate(dentry, inode);
  621. out_commit:
  622. ocfs2_commit_trans(osb, handle);
  623. out_unlock_inode:
  624. ocfs2_inode_unlock(inode, 1);
  625. out:
  626. ocfs2_inode_unlock(dir, 1);
  627. brelse(fe_bh);
  628. brelse(parent_fe_bh);
  629. ocfs2_free_dir_lookup_result(&lookup);
  630. mlog_exit(err);
  631. return err;
  632. }
  633. /*
  634. * Takes and drops an exclusive lock on the given dentry. This will
  635. * force other nodes to drop it.
  636. */
  637. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  638. {
  639. int ret;
  640. ret = ocfs2_dentry_lock(dentry, 1);
  641. if (ret)
  642. mlog_errno(ret);
  643. else
  644. ocfs2_dentry_unlock(dentry, 1);
  645. return ret;
  646. }
  647. static inline int inode_is_unlinkable(struct inode *inode)
  648. {
  649. if (S_ISDIR(inode->i_mode)) {
  650. if (inode->i_nlink == 2)
  651. return 1;
  652. return 0;
  653. }
  654. if (inode->i_nlink == 1)
  655. return 1;
  656. return 0;
  657. }
  658. static int ocfs2_unlink(struct inode *dir,
  659. struct dentry *dentry)
  660. {
  661. int status;
  662. int child_locked = 0;
  663. struct inode *inode = dentry->d_inode;
  664. struct inode *orphan_dir = NULL;
  665. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  666. u64 blkno;
  667. struct ocfs2_dinode *fe = NULL;
  668. struct buffer_head *fe_bh = NULL;
  669. struct buffer_head *parent_node_bh = NULL;
  670. handle_t *handle = NULL;
  671. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  672. struct ocfs2_dir_lookup_result lookup = { NULL, };
  673. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  674. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  675. dentry->d_name.len, dentry->d_name.name);
  676. BUG_ON(dentry->d_parent->d_inode != dir);
  677. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  678. if (inode == osb->root_inode) {
  679. mlog(0, "Cannot delete the root directory\n");
  680. return -EPERM;
  681. }
  682. status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
  683. OI_LS_PARENT);
  684. if (status < 0) {
  685. if (status != -ENOENT)
  686. mlog_errno(status);
  687. return status;
  688. }
  689. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  690. dentry->d_name.len, &blkno, dir,
  691. &lookup);
  692. if (status < 0) {
  693. if (status != -ENOENT)
  694. mlog_errno(status);
  695. goto leave;
  696. }
  697. if (OCFS2_I(inode)->ip_blkno != blkno) {
  698. status = -ENOENT;
  699. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  700. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  701. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  702. goto leave;
  703. }
  704. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  705. if (status < 0) {
  706. if (status != -ENOENT)
  707. mlog_errno(status);
  708. goto leave;
  709. }
  710. child_locked = 1;
  711. if (S_ISDIR(inode->i_mode)) {
  712. if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
  713. status = -ENOTEMPTY;
  714. goto leave;
  715. }
  716. }
  717. status = ocfs2_remote_dentry_delete(dentry);
  718. if (status < 0) {
  719. /* This remote delete should succeed under all normal
  720. * circumstances. */
  721. mlog_errno(status);
  722. goto leave;
  723. }
  724. if (inode_is_unlinkable(inode)) {
  725. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
  726. orphan_name, &orphan_insert);
  727. if (status < 0) {
  728. mlog_errno(status);
  729. goto leave;
  730. }
  731. }
  732. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  733. if (IS_ERR(handle)) {
  734. status = PTR_ERR(handle);
  735. handle = NULL;
  736. mlog_errno(status);
  737. goto leave;
  738. }
  739. status = ocfs2_journal_access_di(handle, inode, fe_bh,
  740. OCFS2_JOURNAL_ACCESS_WRITE);
  741. if (status < 0) {
  742. mlog_errno(status);
  743. goto leave;
  744. }
  745. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  746. if (inode_is_unlinkable(inode)) {
  747. status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
  748. &orphan_insert, orphan_dir);
  749. if (status < 0) {
  750. mlog_errno(status);
  751. goto leave;
  752. }
  753. }
  754. /* delete the name from the parent dir */
  755. status = ocfs2_delete_entry(handle, dir, &lookup);
  756. if (status < 0) {
  757. mlog_errno(status);
  758. goto leave;
  759. }
  760. if (S_ISDIR(inode->i_mode))
  761. drop_nlink(inode);
  762. drop_nlink(inode);
  763. ocfs2_set_links_count(fe, inode->i_nlink);
  764. status = ocfs2_journal_dirty(handle, fe_bh);
  765. if (status < 0) {
  766. mlog_errno(status);
  767. goto leave;
  768. }
  769. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  770. if (S_ISDIR(inode->i_mode))
  771. drop_nlink(dir);
  772. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  773. if (status < 0) {
  774. mlog_errno(status);
  775. if (S_ISDIR(inode->i_mode))
  776. inc_nlink(dir);
  777. }
  778. leave:
  779. if (handle)
  780. ocfs2_commit_trans(osb, handle);
  781. if (child_locked)
  782. ocfs2_inode_unlock(inode, 1);
  783. ocfs2_inode_unlock(dir, 1);
  784. if (orphan_dir) {
  785. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  786. ocfs2_inode_unlock(orphan_dir, 1);
  787. mutex_unlock(&orphan_dir->i_mutex);
  788. iput(orphan_dir);
  789. }
  790. brelse(fe_bh);
  791. brelse(parent_node_bh);
  792. ocfs2_free_dir_lookup_result(&orphan_insert);
  793. ocfs2_free_dir_lookup_result(&lookup);
  794. mlog_exit(status);
  795. return status;
  796. }
  797. /*
  798. * The only place this should be used is rename!
  799. * if they have the same id, then the 1st one is the only one locked.
  800. */
  801. static int ocfs2_double_lock(struct ocfs2_super *osb,
  802. struct buffer_head **bh1,
  803. struct inode *inode1,
  804. struct buffer_head **bh2,
  805. struct inode *inode2)
  806. {
  807. int status;
  808. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  809. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  810. struct buffer_head **tmpbh;
  811. struct inode *tmpinode;
  812. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  813. (unsigned long long)oi1->ip_blkno,
  814. (unsigned long long)oi2->ip_blkno);
  815. if (*bh1)
  816. *bh1 = NULL;
  817. if (*bh2)
  818. *bh2 = NULL;
  819. /* we always want to lock the one with the lower lockid first. */
  820. if (oi1->ip_blkno != oi2->ip_blkno) {
  821. if (oi1->ip_blkno < oi2->ip_blkno) {
  822. /* switch id1 and id2 around */
  823. mlog(0, "switching them around...\n");
  824. tmpbh = bh2;
  825. bh2 = bh1;
  826. bh1 = tmpbh;
  827. tmpinode = inode2;
  828. inode2 = inode1;
  829. inode1 = tmpinode;
  830. }
  831. /* lock id2 */
  832. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  833. OI_LS_RENAME1);
  834. if (status < 0) {
  835. if (status != -ENOENT)
  836. mlog_errno(status);
  837. goto bail;
  838. }
  839. }
  840. /* lock id1 */
  841. status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
  842. if (status < 0) {
  843. /*
  844. * An error return must mean that no cluster locks
  845. * were held on function exit.
  846. */
  847. if (oi1->ip_blkno != oi2->ip_blkno)
  848. ocfs2_inode_unlock(inode2, 1);
  849. if (status != -ENOENT)
  850. mlog_errno(status);
  851. }
  852. bail:
  853. mlog_exit(status);
  854. return status;
  855. }
  856. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  857. {
  858. ocfs2_inode_unlock(inode1, 1);
  859. if (inode1 != inode2)
  860. ocfs2_inode_unlock(inode2, 1);
  861. }
  862. static int ocfs2_rename(struct inode *old_dir,
  863. struct dentry *old_dentry,
  864. struct inode *new_dir,
  865. struct dentry *new_dentry)
  866. {
  867. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  868. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  869. struct inode *old_inode = old_dentry->d_inode;
  870. struct inode *new_inode = new_dentry->d_inode;
  871. struct inode *orphan_dir = NULL;
  872. struct ocfs2_dinode *newfe = NULL;
  873. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  874. struct buffer_head *newfe_bh = NULL;
  875. struct buffer_head *old_inode_bh = NULL;
  876. struct ocfs2_super *osb = NULL;
  877. u64 newfe_blkno, old_de_ino;
  878. handle_t *handle = NULL;
  879. struct buffer_head *old_dir_bh = NULL;
  880. struct buffer_head *new_dir_bh = NULL;
  881. nlink_t old_dir_nlink = old_dir->i_nlink;
  882. struct ocfs2_dinode *old_di;
  883. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  884. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  885. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  886. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  887. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  888. /* At some point it might be nice to break this function up a
  889. * bit. */
  890. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  891. old_dir, old_dentry, new_dir, new_dentry,
  892. old_dentry->d_name.len, old_dentry->d_name.name,
  893. new_dentry->d_name.len, new_dentry->d_name.name);
  894. osb = OCFS2_SB(old_dir->i_sb);
  895. if (new_inode) {
  896. if (!igrab(new_inode))
  897. BUG();
  898. }
  899. /* Assume a directory hierarchy thusly:
  900. * a/b/c
  901. * a/d
  902. * a,b,c, and d are all directories.
  903. *
  904. * from cwd of 'a' on both nodes:
  905. * node1: mv b/c d
  906. * node2: mv d b/c
  907. *
  908. * And that's why, just like the VFS, we need a file system
  909. * rename lock. */
  910. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  911. status = ocfs2_rename_lock(osb);
  912. if (status < 0) {
  913. mlog_errno(status);
  914. goto bail;
  915. }
  916. rename_lock = 1;
  917. }
  918. /* if old and new are the same, this'll just do one lock. */
  919. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  920. &new_dir_bh, new_dir);
  921. if (status < 0) {
  922. mlog_errno(status);
  923. goto bail;
  924. }
  925. parents_locked = 1;
  926. /* make sure both dirs have bhs
  927. * get an extra ref on old_dir_bh if old==new */
  928. if (!new_dir_bh) {
  929. if (old_dir_bh) {
  930. new_dir_bh = old_dir_bh;
  931. get_bh(new_dir_bh);
  932. } else {
  933. mlog(ML_ERROR, "no old_dir_bh!\n");
  934. status = -EIO;
  935. goto bail;
  936. }
  937. }
  938. /*
  939. * Aside from allowing a meta data update, the locking here
  940. * also ensures that the downconvert thread on other nodes
  941. * won't have to concurrently downconvert the inode and the
  942. * dentry locks.
  943. */
  944. status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
  945. OI_LS_PARENT);
  946. if (status < 0) {
  947. if (status != -ENOENT)
  948. mlog_errno(status);
  949. goto bail;
  950. }
  951. old_child_locked = 1;
  952. status = ocfs2_remote_dentry_delete(old_dentry);
  953. if (status < 0) {
  954. mlog_errno(status);
  955. goto bail;
  956. }
  957. if (S_ISDIR(old_inode->i_mode)) {
  958. u64 old_inode_parent;
  959. update_dot_dot = 1;
  960. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  961. old_inode,
  962. &old_inode_dot_dot_res);
  963. if (status) {
  964. status = -EIO;
  965. goto bail;
  966. }
  967. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  968. status = -EIO;
  969. goto bail;
  970. }
  971. if (!new_inode && new_dir != old_dir &&
  972. new_dir->i_nlink >= ocfs2_link_max(osb)) {
  973. status = -EMLINK;
  974. goto bail;
  975. }
  976. }
  977. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  978. old_dentry->d_name.len,
  979. &old_de_ino);
  980. if (status) {
  981. status = -ENOENT;
  982. goto bail;
  983. }
  984. /*
  985. * Check for inode number is _not_ due to possible IO errors.
  986. * We might rmdir the source, keep it as pwd of some process
  987. * and merrily kill the link to whatever was created under the
  988. * same name. Goodbye sticky bit ;-<
  989. */
  990. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  991. status = -ENOENT;
  992. goto bail;
  993. }
  994. /* check if the target already exists (in which case we need
  995. * to delete it */
  996. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  997. new_dentry->d_name.len,
  998. &newfe_blkno, new_dir,
  999. &target_lookup_res);
  1000. /* The only error we allow here is -ENOENT because the new
  1001. * file not existing is perfectly valid. */
  1002. if ((status < 0) && (status != -ENOENT)) {
  1003. /* If we cannot find the file specified we should just */
  1004. /* return the error... */
  1005. mlog_errno(status);
  1006. goto bail;
  1007. }
  1008. if (status == 0)
  1009. target_exists = 1;
  1010. if (!target_exists && new_inode) {
  1011. /*
  1012. * Target was unlinked by another node while we were
  1013. * waiting to get to ocfs2_rename(). There isn't
  1014. * anything we can do here to help the situation, so
  1015. * bubble up the appropriate error.
  1016. */
  1017. status = -ENOENT;
  1018. goto bail;
  1019. }
  1020. /* In case we need to overwrite an existing file, we blow it
  1021. * away first */
  1022. if (target_exists) {
  1023. /* VFS didn't think there existed an inode here, but
  1024. * someone else in the cluster must have raced our
  1025. * rename to create one. Today we error cleanly, in
  1026. * the future we should consider calling iget to build
  1027. * a new struct inode for this entry. */
  1028. if (!new_inode) {
  1029. status = -EACCES;
  1030. mlog(0, "We found an inode for name %.*s but VFS "
  1031. "didn't give us one.\n", new_dentry->d_name.len,
  1032. new_dentry->d_name.name);
  1033. goto bail;
  1034. }
  1035. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1036. status = -EACCES;
  1037. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  1038. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1039. (unsigned long long)newfe_blkno,
  1040. OCFS2_I(new_inode)->ip_flags);
  1041. goto bail;
  1042. }
  1043. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1044. if (status < 0) {
  1045. if (status != -ENOENT)
  1046. mlog_errno(status);
  1047. goto bail;
  1048. }
  1049. new_child_locked = 1;
  1050. status = ocfs2_remote_dentry_delete(new_dentry);
  1051. if (status < 0) {
  1052. mlog_errno(status);
  1053. goto bail;
  1054. }
  1055. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1056. mlog(0, "aha rename over existing... new_blkno=%llu "
  1057. "newfebh=%p bhblocknr=%llu\n",
  1058. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1059. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1060. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1061. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1062. new_inode,
  1063. orphan_name,
  1064. &orphan_insert);
  1065. if (status < 0) {
  1066. mlog_errno(status);
  1067. goto bail;
  1068. }
  1069. }
  1070. } else {
  1071. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1072. status = ocfs2_check_dir_for_entry(new_dir,
  1073. new_dentry->d_name.name,
  1074. new_dentry->d_name.len);
  1075. if (status)
  1076. goto bail;
  1077. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1078. new_dentry->d_name.name,
  1079. new_dentry->d_name.len,
  1080. &target_insert);
  1081. if (status < 0) {
  1082. mlog_errno(status);
  1083. goto bail;
  1084. }
  1085. }
  1086. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1087. if (IS_ERR(handle)) {
  1088. status = PTR_ERR(handle);
  1089. handle = NULL;
  1090. mlog_errno(status);
  1091. goto bail;
  1092. }
  1093. if (target_exists) {
  1094. if (S_ISDIR(new_inode->i_mode)) {
  1095. if (new_inode->i_nlink != 2 ||
  1096. !ocfs2_empty_dir(new_inode)) {
  1097. status = -ENOTEMPTY;
  1098. goto bail;
  1099. }
  1100. }
  1101. status = ocfs2_journal_access_di(handle, new_inode, newfe_bh,
  1102. OCFS2_JOURNAL_ACCESS_WRITE);
  1103. if (status < 0) {
  1104. mlog_errno(status);
  1105. goto bail;
  1106. }
  1107. if (S_ISDIR(new_inode->i_mode) ||
  1108. (ocfs2_read_links_count(newfe) == 1)) {
  1109. status = ocfs2_orphan_add(osb, handle, new_inode,
  1110. newfe, orphan_name,
  1111. &orphan_insert, orphan_dir);
  1112. if (status < 0) {
  1113. mlog_errno(status);
  1114. goto bail;
  1115. }
  1116. }
  1117. /* change the dirent to point to the correct inode */
  1118. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1119. old_inode);
  1120. if (status < 0) {
  1121. mlog_errno(status);
  1122. goto bail;
  1123. }
  1124. new_dir->i_version++;
  1125. if (S_ISDIR(new_inode->i_mode))
  1126. ocfs2_set_links_count(newfe, 0);
  1127. else
  1128. ocfs2_add_links_count(newfe, -1);
  1129. status = ocfs2_journal_dirty(handle, newfe_bh);
  1130. if (status < 0) {
  1131. mlog_errno(status);
  1132. goto bail;
  1133. }
  1134. } else {
  1135. /* if the name was not found in new_dir, add it now */
  1136. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1137. OCFS2_I(old_inode)->ip_blkno,
  1138. new_dir_bh, &target_insert);
  1139. }
  1140. old_inode->i_ctime = CURRENT_TIME;
  1141. mark_inode_dirty(old_inode);
  1142. status = ocfs2_journal_access_di(handle, old_inode, old_inode_bh,
  1143. OCFS2_JOURNAL_ACCESS_WRITE);
  1144. if (status >= 0) {
  1145. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1146. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1147. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1148. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1149. if (status < 0)
  1150. mlog_errno(status);
  1151. } else
  1152. mlog_errno(status);
  1153. /*
  1154. * Now that the name has been added to new_dir, remove the old name.
  1155. *
  1156. * We don't keep any directory entry context around until now
  1157. * because the insert might have changed the type of directory
  1158. * we're dealing with.
  1159. */
  1160. status = ocfs2_find_entry(old_dentry->d_name.name,
  1161. old_dentry->d_name.len, old_dir,
  1162. &old_entry_lookup);
  1163. if (status)
  1164. goto bail;
  1165. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1166. if (status < 0) {
  1167. mlog_errno(status);
  1168. goto bail;
  1169. }
  1170. if (new_inode) {
  1171. new_inode->i_nlink--;
  1172. new_inode->i_ctime = CURRENT_TIME;
  1173. }
  1174. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1175. if (update_dot_dot) {
  1176. status = ocfs2_update_entry(old_inode, handle,
  1177. &old_inode_dot_dot_res, new_dir);
  1178. old_dir->i_nlink--;
  1179. if (new_inode) {
  1180. new_inode->i_nlink--;
  1181. } else {
  1182. inc_nlink(new_dir);
  1183. mark_inode_dirty(new_dir);
  1184. }
  1185. }
  1186. mark_inode_dirty(old_dir);
  1187. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1188. if (new_inode) {
  1189. mark_inode_dirty(new_inode);
  1190. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1191. }
  1192. if (old_dir != new_dir) {
  1193. /* Keep the same times on both directories.*/
  1194. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1195. /*
  1196. * This will also pick up the i_nlink change from the
  1197. * block above.
  1198. */
  1199. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1200. }
  1201. if (old_dir_nlink != old_dir->i_nlink) {
  1202. if (!old_dir_bh) {
  1203. mlog(ML_ERROR, "need to change nlink for old dir "
  1204. "%llu from %d to %d but bh is NULL!\n",
  1205. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1206. (int)old_dir_nlink, old_dir->i_nlink);
  1207. } else {
  1208. struct ocfs2_dinode *fe;
  1209. status = ocfs2_journal_access_di(handle, old_dir,
  1210. old_dir_bh,
  1211. OCFS2_JOURNAL_ACCESS_WRITE);
  1212. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1213. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1214. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1215. }
  1216. }
  1217. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1218. status = 0;
  1219. bail:
  1220. if (rename_lock)
  1221. ocfs2_rename_unlock(osb);
  1222. if (handle)
  1223. ocfs2_commit_trans(osb, handle);
  1224. if (parents_locked)
  1225. ocfs2_double_unlock(old_dir, new_dir);
  1226. if (old_child_locked)
  1227. ocfs2_inode_unlock(old_inode, 1);
  1228. if (new_child_locked)
  1229. ocfs2_inode_unlock(new_inode, 1);
  1230. if (orphan_dir) {
  1231. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1232. ocfs2_inode_unlock(orphan_dir, 1);
  1233. mutex_unlock(&orphan_dir->i_mutex);
  1234. iput(orphan_dir);
  1235. }
  1236. if (new_inode)
  1237. sync_mapping_buffers(old_inode->i_mapping);
  1238. if (new_inode)
  1239. iput(new_inode);
  1240. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1241. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1242. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1243. ocfs2_free_dir_lookup_result(&orphan_insert);
  1244. ocfs2_free_dir_lookup_result(&target_insert);
  1245. brelse(newfe_bh);
  1246. brelse(old_inode_bh);
  1247. brelse(old_dir_bh);
  1248. brelse(new_dir_bh);
  1249. mlog_exit(status);
  1250. return status;
  1251. }
  1252. /*
  1253. * we expect i_size = strlen(symname). Copy symname into the file
  1254. * data, including the null terminator.
  1255. */
  1256. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1257. handle_t *handle,
  1258. struct inode *inode,
  1259. const char *symname)
  1260. {
  1261. struct buffer_head **bhs = NULL;
  1262. const char *c;
  1263. struct super_block *sb = osb->sb;
  1264. u64 p_blkno, p_blocks;
  1265. int virtual, blocks, status, i, bytes_left;
  1266. bytes_left = i_size_read(inode) + 1;
  1267. /* we can't trust i_blocks because we're actually going to
  1268. * write i_size + 1 bytes. */
  1269. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1270. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1271. (unsigned long long)inode->i_blocks,
  1272. i_size_read(inode), blocks);
  1273. /* Sanity check -- make sure we're going to fit. */
  1274. if (bytes_left >
  1275. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1276. status = -EIO;
  1277. mlog_errno(status);
  1278. goto bail;
  1279. }
  1280. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1281. if (!bhs) {
  1282. status = -ENOMEM;
  1283. mlog_errno(status);
  1284. goto bail;
  1285. }
  1286. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1287. NULL);
  1288. if (status < 0) {
  1289. mlog_errno(status);
  1290. goto bail;
  1291. }
  1292. /* links can never be larger than one cluster so we know this
  1293. * is all going to be contiguous, but do a sanity check
  1294. * anyway. */
  1295. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1296. status = -EIO;
  1297. mlog_errno(status);
  1298. goto bail;
  1299. }
  1300. virtual = 0;
  1301. while(bytes_left > 0) {
  1302. c = &symname[virtual * sb->s_blocksize];
  1303. bhs[virtual] = sb_getblk(sb, p_blkno);
  1304. if (!bhs[virtual]) {
  1305. status = -ENOMEM;
  1306. mlog_errno(status);
  1307. goto bail;
  1308. }
  1309. ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
  1310. status = ocfs2_journal_access(handle, inode, bhs[virtual],
  1311. OCFS2_JOURNAL_ACCESS_CREATE);
  1312. if (status < 0) {
  1313. mlog_errno(status);
  1314. goto bail;
  1315. }
  1316. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1317. memcpy(bhs[virtual]->b_data, c,
  1318. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1319. bytes_left);
  1320. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1321. if (status < 0) {
  1322. mlog_errno(status);
  1323. goto bail;
  1324. }
  1325. virtual++;
  1326. p_blkno++;
  1327. bytes_left -= sb->s_blocksize;
  1328. }
  1329. status = 0;
  1330. bail:
  1331. if (bhs) {
  1332. for(i = 0; i < blocks; i++)
  1333. brelse(bhs[i]);
  1334. kfree(bhs);
  1335. }
  1336. mlog_exit(status);
  1337. return status;
  1338. }
  1339. static int ocfs2_symlink(struct inode *dir,
  1340. struct dentry *dentry,
  1341. const char *symname)
  1342. {
  1343. int status, l, credits;
  1344. u64 newsize;
  1345. struct ocfs2_super *osb = NULL;
  1346. struct inode *inode = NULL;
  1347. struct super_block *sb;
  1348. struct buffer_head *new_fe_bh = NULL;
  1349. struct buffer_head *parent_fe_bh = NULL;
  1350. struct ocfs2_dinode *fe = NULL;
  1351. struct ocfs2_dinode *dirfe;
  1352. handle_t *handle = NULL;
  1353. struct ocfs2_alloc_context *inode_ac = NULL;
  1354. struct ocfs2_alloc_context *data_ac = NULL;
  1355. struct ocfs2_alloc_context *xattr_ac = NULL;
  1356. int want_clusters = 0;
  1357. int xattr_credits = 0;
  1358. struct ocfs2_security_xattr_info si = {
  1359. .enable = 1,
  1360. };
  1361. int did_quota = 0, did_quota_inode = 0;
  1362. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1363. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1364. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1365. sb = dir->i_sb;
  1366. osb = OCFS2_SB(sb);
  1367. l = strlen(symname) + 1;
  1368. credits = ocfs2_calc_symlink_credits(sb);
  1369. /* lock the parent directory */
  1370. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1371. if (status < 0) {
  1372. if (status != -ENOENT)
  1373. mlog_errno(status);
  1374. return status;
  1375. }
  1376. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1377. if (!ocfs2_read_links_count(dirfe)) {
  1378. /* can't make a file in a deleted directory. */
  1379. status = -ENOENT;
  1380. goto bail;
  1381. }
  1382. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1383. dentry->d_name.len);
  1384. if (status)
  1385. goto bail;
  1386. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1387. dentry->d_name.name,
  1388. dentry->d_name.len, &lookup);
  1389. if (status < 0) {
  1390. mlog_errno(status);
  1391. goto bail;
  1392. }
  1393. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1394. if (status < 0) {
  1395. if (status != -ENOSPC)
  1396. mlog_errno(status);
  1397. goto bail;
  1398. }
  1399. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1400. if (!inode) {
  1401. status = -ENOMEM;
  1402. mlog_errno(status);
  1403. goto bail;
  1404. }
  1405. /* get security xattr */
  1406. status = ocfs2_init_security_get(inode, dir, &si);
  1407. if (status) {
  1408. if (status == -EOPNOTSUPP)
  1409. si.enable = 0;
  1410. else {
  1411. mlog_errno(status);
  1412. goto bail;
  1413. }
  1414. }
  1415. /* calculate meta data/clusters for setting security xattr */
  1416. if (si.enable) {
  1417. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1418. &xattr_credits, &xattr_ac);
  1419. if (status < 0) {
  1420. mlog_errno(status);
  1421. goto bail;
  1422. }
  1423. }
  1424. /* don't reserve bitmap space for fast symlinks. */
  1425. if (l > ocfs2_fast_symlink_chars(sb))
  1426. want_clusters += 1;
  1427. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1428. if (status < 0) {
  1429. if (status != -ENOSPC)
  1430. mlog_errno(status);
  1431. goto bail;
  1432. }
  1433. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1434. if (IS_ERR(handle)) {
  1435. status = PTR_ERR(handle);
  1436. handle = NULL;
  1437. mlog_errno(status);
  1438. goto bail;
  1439. }
  1440. /* We don't use standard VFS wrapper because we don't want vfs_dq_init
  1441. * to be called. */
  1442. if (sb_any_quota_active(osb->sb) &&
  1443. osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
  1444. status = -EDQUOT;
  1445. goto bail;
  1446. }
  1447. did_quota_inode = 1;
  1448. status = ocfs2_mknod_locked(osb, dir, inode, dentry,
  1449. 0, &new_fe_bh, parent_fe_bh, handle,
  1450. inode_ac);
  1451. if (status < 0) {
  1452. mlog_errno(status);
  1453. goto bail;
  1454. }
  1455. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1456. inode->i_rdev = 0;
  1457. newsize = l - 1;
  1458. if (l > ocfs2_fast_symlink_chars(sb)) {
  1459. u32 offset = 0;
  1460. inode->i_op = &ocfs2_symlink_inode_operations;
  1461. if (vfs_dq_alloc_space_nodirty(inode,
  1462. ocfs2_clusters_to_bytes(osb->sb, 1))) {
  1463. status = -EDQUOT;
  1464. goto bail;
  1465. }
  1466. did_quota = 1;
  1467. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1468. new_fe_bh,
  1469. handle, data_ac, NULL,
  1470. NULL);
  1471. if (status < 0) {
  1472. if (status != -ENOSPC && status != -EINTR) {
  1473. mlog(ML_ERROR,
  1474. "Failed to extend file to %llu\n",
  1475. (unsigned long long)newsize);
  1476. mlog_errno(status);
  1477. status = -ENOSPC;
  1478. }
  1479. goto bail;
  1480. }
  1481. i_size_write(inode, newsize);
  1482. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1483. } else {
  1484. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1485. memcpy((char *) fe->id2.i_symlink, symname, l);
  1486. i_size_write(inode, newsize);
  1487. inode->i_blocks = 0;
  1488. }
  1489. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1490. if (status < 0) {
  1491. mlog_errno(status);
  1492. goto bail;
  1493. }
  1494. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1495. status = ocfs2_create_symlink_data(osb, handle, inode,
  1496. symname);
  1497. if (status < 0) {
  1498. mlog_errno(status);
  1499. goto bail;
  1500. }
  1501. }
  1502. if (si.enable) {
  1503. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1504. xattr_ac, data_ac);
  1505. if (status < 0) {
  1506. mlog_errno(status);
  1507. goto bail;
  1508. }
  1509. }
  1510. status = ocfs2_add_entry(handle, dentry, inode,
  1511. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1512. &lookup);
  1513. if (status < 0) {
  1514. mlog_errno(status);
  1515. goto bail;
  1516. }
  1517. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1518. if (status) {
  1519. mlog_errno(status);
  1520. goto bail;
  1521. }
  1522. insert_inode_hash(inode);
  1523. dentry->d_op = &ocfs2_dentry_ops;
  1524. d_instantiate(dentry, inode);
  1525. bail:
  1526. if (status < 0 && did_quota)
  1527. vfs_dq_free_space_nodirty(inode,
  1528. ocfs2_clusters_to_bytes(osb->sb, 1));
  1529. if (status < 0 && did_quota_inode)
  1530. vfs_dq_free_inode(inode);
  1531. if (handle)
  1532. ocfs2_commit_trans(osb, handle);
  1533. ocfs2_inode_unlock(dir, 1);
  1534. brelse(new_fe_bh);
  1535. brelse(parent_fe_bh);
  1536. kfree(si.name);
  1537. kfree(si.value);
  1538. ocfs2_free_dir_lookup_result(&lookup);
  1539. if (inode_ac)
  1540. ocfs2_free_alloc_context(inode_ac);
  1541. if (data_ac)
  1542. ocfs2_free_alloc_context(data_ac);
  1543. if (xattr_ac)
  1544. ocfs2_free_alloc_context(xattr_ac);
  1545. if ((status < 0) && inode) {
  1546. clear_nlink(inode);
  1547. iput(inode);
  1548. }
  1549. mlog_exit(status);
  1550. return status;
  1551. }
  1552. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1553. {
  1554. int status, namelen;
  1555. mlog_entry_void();
  1556. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1557. (long long)blkno);
  1558. if (namelen <= 0) {
  1559. if (namelen)
  1560. status = namelen;
  1561. else
  1562. status = -EINVAL;
  1563. mlog_errno(status);
  1564. goto bail;
  1565. }
  1566. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1567. status = -EINVAL;
  1568. mlog_errno(status);
  1569. goto bail;
  1570. }
  1571. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1572. namelen);
  1573. status = 0;
  1574. bail:
  1575. mlog_exit(status);
  1576. return status;
  1577. }
  1578. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1579. struct inode **ret_orphan_dir,
  1580. struct inode *inode,
  1581. char *name,
  1582. struct ocfs2_dir_lookup_result *lookup)
  1583. {
  1584. struct inode *orphan_dir_inode;
  1585. struct buffer_head *orphan_dir_bh = NULL;
  1586. int status = 0;
  1587. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1588. if (status < 0) {
  1589. mlog_errno(status);
  1590. return status;
  1591. }
  1592. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1593. ORPHAN_DIR_SYSTEM_INODE,
  1594. osb->slot_num);
  1595. if (!orphan_dir_inode) {
  1596. status = -ENOENT;
  1597. mlog_errno(status);
  1598. return status;
  1599. }
  1600. mutex_lock(&orphan_dir_inode->i_mutex);
  1601. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1602. if (status < 0) {
  1603. mlog_errno(status);
  1604. goto leave;
  1605. }
  1606. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1607. orphan_dir_bh, name,
  1608. OCFS2_ORPHAN_NAMELEN, lookup);
  1609. if (status < 0) {
  1610. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1611. mlog_errno(status);
  1612. goto leave;
  1613. }
  1614. *ret_orphan_dir = orphan_dir_inode;
  1615. leave:
  1616. if (status) {
  1617. mutex_unlock(&orphan_dir_inode->i_mutex);
  1618. iput(orphan_dir_inode);
  1619. }
  1620. brelse(orphan_dir_bh);
  1621. mlog_exit(status);
  1622. return status;
  1623. }
  1624. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1625. handle_t *handle,
  1626. struct inode *inode,
  1627. struct ocfs2_dinode *fe,
  1628. char *name,
  1629. struct ocfs2_dir_lookup_result *lookup,
  1630. struct inode *orphan_dir_inode)
  1631. {
  1632. struct buffer_head *orphan_dir_bh = NULL;
  1633. int status = 0;
  1634. struct ocfs2_dinode *orphan_fe;
  1635. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1636. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1637. if (status < 0) {
  1638. mlog_errno(status);
  1639. goto leave;
  1640. }
  1641. status = ocfs2_journal_access_di(handle, orphan_dir_inode, orphan_dir_bh,
  1642. OCFS2_JOURNAL_ACCESS_WRITE);
  1643. if (status < 0) {
  1644. mlog_errno(status);
  1645. goto leave;
  1646. }
  1647. /* we're a cluster, and nlink can change on disk from
  1648. * underneath us... */
  1649. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1650. if (S_ISDIR(inode->i_mode))
  1651. ocfs2_add_links_count(orphan_fe, 1);
  1652. orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
  1653. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1654. if (status < 0) {
  1655. mlog_errno(status);
  1656. goto leave;
  1657. }
  1658. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1659. OCFS2_ORPHAN_NAMELEN, inode,
  1660. OCFS2_I(inode)->ip_blkno,
  1661. orphan_dir_bh, lookup);
  1662. if (status < 0) {
  1663. mlog_errno(status);
  1664. goto leave;
  1665. }
  1666. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1667. /* Record which orphan dir our inode now resides
  1668. * in. delete_inode will use this to determine which orphan
  1669. * dir to lock. */
  1670. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1671. mlog(0, "Inode %llu orphaned in slot %d\n",
  1672. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1673. leave:
  1674. brelse(orphan_dir_bh);
  1675. mlog_exit(status);
  1676. return status;
  1677. }
  1678. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1679. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1680. handle_t *handle,
  1681. struct inode *orphan_dir_inode,
  1682. struct inode *inode,
  1683. struct buffer_head *orphan_dir_bh)
  1684. {
  1685. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1686. struct ocfs2_dinode *orphan_fe;
  1687. int status = 0;
  1688. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1689. mlog_entry_void();
  1690. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1691. if (status < 0) {
  1692. mlog_errno(status);
  1693. goto leave;
  1694. }
  1695. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1696. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1697. OCFS2_ORPHAN_NAMELEN);
  1698. /* find it's spot in the orphan directory */
  1699. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1700. &lookup);
  1701. if (status) {
  1702. mlog_errno(status);
  1703. goto leave;
  1704. }
  1705. /* remove it from the orphan directory */
  1706. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1707. if (status < 0) {
  1708. mlog_errno(status);
  1709. goto leave;
  1710. }
  1711. status = ocfs2_journal_access_di(handle,orphan_dir_inode, orphan_dir_bh,
  1712. OCFS2_JOURNAL_ACCESS_WRITE);
  1713. if (status < 0) {
  1714. mlog_errno(status);
  1715. goto leave;
  1716. }
  1717. /* do the i_nlink dance! :) */
  1718. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1719. if (S_ISDIR(inode->i_mode))
  1720. ocfs2_add_links_count(orphan_fe, -1);
  1721. orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
  1722. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1723. if (status < 0) {
  1724. mlog_errno(status);
  1725. goto leave;
  1726. }
  1727. leave:
  1728. ocfs2_free_dir_lookup_result(&lookup);
  1729. mlog_exit(status);
  1730. return status;
  1731. }
  1732. const struct inode_operations ocfs2_dir_iops = {
  1733. .create = ocfs2_create,
  1734. .lookup = ocfs2_lookup,
  1735. .link = ocfs2_link,
  1736. .unlink = ocfs2_unlink,
  1737. .rmdir = ocfs2_unlink,
  1738. .symlink = ocfs2_symlink,
  1739. .mkdir = ocfs2_mkdir,
  1740. .mknod = ocfs2_mknod,
  1741. .rename = ocfs2_rename,
  1742. .setattr = ocfs2_setattr,
  1743. .getattr = ocfs2_getattr,
  1744. .permission = ocfs2_permission,
  1745. .setxattr = generic_setxattr,
  1746. .getxattr = generic_getxattr,
  1747. .listxattr = ocfs2_listxattr,
  1748. .removexattr = generic_removexattr,
  1749. };