namei.c 48 KB

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