namei.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  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. dev_t dev,
  67. struct buffer_head **new_fe_bh,
  68. struct buffer_head *parent_fe_bh,
  69. handle_t *handle,
  70. struct ocfs2_alloc_context *inode_ac);
  71. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  72. struct inode **ret_orphan_dir,
  73. u64 blkno,
  74. char *name,
  75. struct ocfs2_dir_lookup_result *lookup);
  76. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  77. handle_t *handle,
  78. struct inode *inode,
  79. struct buffer_head *fe_bh,
  80. char *name,
  81. struct ocfs2_dir_lookup_result *lookup,
  82. struct inode *orphan_dir_inode);
  83. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  84. handle_t *handle,
  85. struct inode *inode,
  86. const char *symname);
  87. /* An orphan dir name is an 8 byte value, printed as a hex string */
  88. #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
  89. static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
  90. struct nameidata *nd)
  91. {
  92. int status;
  93. u64 blkno;
  94. struct inode *inode = NULL;
  95. struct dentry *ret;
  96. struct ocfs2_inode_info *oi;
  97. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  98. dentry->d_name.len, dentry->d_name.name);
  99. if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
  100. ret = ERR_PTR(-ENAMETOOLONG);
  101. goto bail;
  102. }
  103. mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
  104. dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  105. status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
  106. if (status < 0) {
  107. if (status != -ENOENT)
  108. mlog_errno(status);
  109. ret = ERR_PTR(status);
  110. goto bail;
  111. }
  112. status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
  113. dentry->d_name.len, &blkno);
  114. if (status < 0)
  115. goto bail_add;
  116. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
  117. if (IS_ERR(inode)) {
  118. ret = ERR_PTR(-EACCES);
  119. goto bail_unlock;
  120. }
  121. oi = OCFS2_I(inode);
  122. /* Clear any orphaned state... If we were able to look up the
  123. * inode from a directory, it certainly can't be orphaned. We
  124. * might have the bad state from a node which intended to
  125. * orphan this inode but crashed before it could commit the
  126. * unlink. */
  127. spin_lock(&oi->ip_lock);
  128. oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
  129. spin_unlock(&oi->ip_lock);
  130. bail_add:
  131. dentry->d_op = &ocfs2_dentry_ops;
  132. ret = d_splice_alias(inode, dentry);
  133. if (inode) {
  134. /*
  135. * If d_splice_alias() finds a DCACHE_DISCONNECTED
  136. * dentry, it will d_move() it on top of ourse. The
  137. * return value will indicate this however, so in
  138. * those cases, we switch them around for the locking
  139. * code.
  140. *
  141. * NOTE: This dentry already has ->d_op set from
  142. * ocfs2_get_parent() and ocfs2_get_dentry()
  143. */
  144. if (ret)
  145. dentry = ret;
  146. status = ocfs2_dentry_attach_lock(dentry, inode,
  147. OCFS2_I(dir)->ip_blkno);
  148. if (status) {
  149. mlog_errno(status);
  150. ret = ERR_PTR(status);
  151. goto bail_unlock;
  152. }
  153. }
  154. bail_unlock:
  155. /* Don't drop the cluster lock until *after* the d_add --
  156. * unlink on another node will message us to remove that
  157. * dentry under this lock so otherwise we can race this with
  158. * the downconvert thread and have a stale dentry. */
  159. ocfs2_inode_unlock(dir, 0);
  160. bail:
  161. mlog_exit_ptr(ret);
  162. return ret;
  163. }
  164. static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
  165. {
  166. struct inode *inode;
  167. inode = new_inode(dir->i_sb);
  168. if (!inode) {
  169. mlog(ML_ERROR, "new_inode failed!\n");
  170. return NULL;
  171. }
  172. /* populate as many fields early on as possible - many of
  173. * these are used by the support functions here and in
  174. * callers. */
  175. if (S_ISDIR(mode))
  176. inode->i_nlink = 2;
  177. else
  178. inode->i_nlink = 1;
  179. inode->i_uid = current_fsuid();
  180. if (dir->i_mode & S_ISGID) {
  181. inode->i_gid = dir->i_gid;
  182. if (S_ISDIR(mode))
  183. mode |= S_ISGID;
  184. } else
  185. inode->i_gid = current_fsgid();
  186. inode->i_mode = mode;
  187. dquot_initialize(inode);
  188. return inode;
  189. }
  190. static int ocfs2_mknod(struct inode *dir,
  191. struct dentry *dentry,
  192. int mode,
  193. dev_t dev)
  194. {
  195. int status = 0;
  196. struct buffer_head *parent_fe_bh = NULL;
  197. handle_t *handle = NULL;
  198. struct ocfs2_super *osb;
  199. struct ocfs2_dinode *dirfe;
  200. struct buffer_head *new_fe_bh = NULL;
  201. struct inode *inode = NULL;
  202. struct ocfs2_alloc_context *inode_ac = NULL;
  203. struct ocfs2_alloc_context *data_ac = NULL;
  204. struct ocfs2_alloc_context *meta_ac = NULL;
  205. int want_clusters = 0;
  206. int want_meta = 0;
  207. int xattr_credits = 0;
  208. struct ocfs2_security_xattr_info si = {
  209. .enable = 1,
  210. };
  211. int did_quota_inode = 0;
  212. struct ocfs2_dir_lookup_result lookup = { NULL, };
  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. dquot_initialize(dir);
  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. status = dquot_alloc_inode(inode);
  307. if (status)
  308. goto leave;
  309. did_quota_inode = 1;
  310. mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
  311. inode->i_mode, (unsigned long)dev, dentry->d_name.len,
  312. dentry->d_name.name);
  313. /* do the real work now. */
  314. status = ocfs2_mknod_locked(osb, dir, inode, dev,
  315. &new_fe_bh, parent_fe_bh, handle,
  316. inode_ac);
  317. if (status < 0) {
  318. mlog_errno(status);
  319. goto leave;
  320. }
  321. if (S_ISDIR(mode)) {
  322. status = ocfs2_fill_new_dir(osb, handle, dir, inode,
  323. new_fe_bh, data_ac, meta_ac);
  324. if (status < 0) {
  325. mlog_errno(status);
  326. goto leave;
  327. }
  328. status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
  329. 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. dquot_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 (inode_ac)
  388. ocfs2_free_alloc_context(inode_ac);
  389. if (data_ac)
  390. ocfs2_free_alloc_context(data_ac);
  391. if (meta_ac)
  392. ocfs2_free_alloc_context(meta_ac);
  393. /*
  394. * We should call iput after the i_mutex of the bitmap been
  395. * unlocked in ocfs2_free_alloc_context, or the
  396. * ocfs2_delete_inode will mutex_lock again.
  397. */
  398. if ((status < 0) && inode) {
  399. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  400. clear_nlink(inode);
  401. iput(inode);
  402. }
  403. mlog_exit(status);
  404. return status;
  405. }
  406. static int ocfs2_mknod_locked(struct ocfs2_super *osb,
  407. struct inode *dir,
  408. struct inode *inode,
  409. dev_t dev,
  410. struct buffer_head **new_fe_bh,
  411. struct buffer_head *parent_fe_bh,
  412. handle_t *handle,
  413. struct ocfs2_alloc_context *inode_ac)
  414. {
  415. int status = 0;
  416. struct ocfs2_dinode *fe = NULL;
  417. struct ocfs2_extent_list *fel;
  418. u64 fe_blkno = 0;
  419. u16 suballoc_bit;
  420. u16 feat;
  421. *new_fe_bh = NULL;
  422. status = ocfs2_claim_new_inode(osb, handle, dir, parent_fe_bh,
  423. inode_ac, &suballoc_bit, &fe_blkno);
  424. if (status < 0) {
  425. mlog_errno(status);
  426. goto leave;
  427. }
  428. /* populate as many fields early on as possible - many of
  429. * these are used by the support functions here and in
  430. * callers. */
  431. inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
  432. OCFS2_I(inode)->ip_blkno = fe_blkno;
  433. spin_lock(&osb->osb_lock);
  434. inode->i_generation = osb->s_next_generation++;
  435. spin_unlock(&osb->osb_lock);
  436. *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
  437. if (!*new_fe_bh) {
  438. status = -EIO;
  439. mlog_errno(status);
  440. goto leave;
  441. }
  442. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
  443. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  444. *new_fe_bh,
  445. OCFS2_JOURNAL_ACCESS_CREATE);
  446. if (status < 0) {
  447. mlog_errno(status);
  448. goto leave;
  449. }
  450. fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
  451. memset(fe, 0, osb->sb->s_blocksize);
  452. fe->i_generation = cpu_to_le32(inode->i_generation);
  453. fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
  454. fe->i_blkno = cpu_to_le64(fe_blkno);
  455. fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
  456. fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
  457. fe->i_uid = cpu_to_le32(inode->i_uid);
  458. fe->i_gid = cpu_to_le32(inode->i_gid);
  459. fe->i_mode = cpu_to_le16(inode->i_mode);
  460. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  461. fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
  462. ocfs2_set_links_count(fe, inode->i_nlink);
  463. fe->i_last_eb_blk = 0;
  464. strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
  465. le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
  466. fe->i_atime = fe->i_ctime = fe->i_mtime =
  467. cpu_to_le64(CURRENT_TIME.tv_sec);
  468. fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
  469. cpu_to_le32(CURRENT_TIME.tv_nsec);
  470. fe->i_dtime = 0;
  471. /*
  472. * If supported, directories start with inline data. If inline
  473. * isn't supported, but indexing is, we start them as indexed.
  474. */
  475. feat = le16_to_cpu(fe->i_dyn_features);
  476. if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
  477. fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
  478. fe->id2.i_data.id_count = cpu_to_le16(
  479. ocfs2_max_inline_data_with_xattr(osb->sb, fe));
  480. } else {
  481. fel = &fe->id2.i_list;
  482. fel->l_tree_depth = 0;
  483. fel->l_next_free_rec = 0;
  484. fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
  485. }
  486. status = ocfs2_journal_dirty(handle, *new_fe_bh);
  487. if (status < 0) {
  488. mlog_errno(status);
  489. goto leave;
  490. }
  491. ocfs2_populate_inode(inode, fe, 1);
  492. ocfs2_ci_set_new(osb, INODE_CACHE(inode));
  493. if (!ocfs2_mount_local(osb)) {
  494. status = ocfs2_create_new_inode_locks(inode);
  495. if (status < 0)
  496. mlog_errno(status);
  497. }
  498. status = 0; /* error in ocfs2_create_new_inode_locks is not
  499. * critical */
  500. leave:
  501. if (status < 0) {
  502. if (*new_fe_bh) {
  503. brelse(*new_fe_bh);
  504. *new_fe_bh = NULL;
  505. }
  506. }
  507. mlog_exit(status);
  508. return status;
  509. }
  510. static int ocfs2_mkdir(struct inode *dir,
  511. struct dentry *dentry,
  512. int mode)
  513. {
  514. int ret;
  515. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  516. dentry->d_name.len, dentry->d_name.name);
  517. ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
  518. mlog_exit(ret);
  519. return ret;
  520. }
  521. static int ocfs2_create(struct inode *dir,
  522. struct dentry *dentry,
  523. int mode,
  524. struct nameidata *nd)
  525. {
  526. int ret;
  527. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
  528. dentry->d_name.len, dentry->d_name.name);
  529. ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
  530. mlog_exit(ret);
  531. return ret;
  532. }
  533. static int ocfs2_link(struct dentry *old_dentry,
  534. struct inode *dir,
  535. struct dentry *dentry)
  536. {
  537. handle_t *handle;
  538. struct inode *inode = old_dentry->d_inode;
  539. int err;
  540. struct buffer_head *fe_bh = NULL;
  541. struct buffer_head *parent_fe_bh = NULL;
  542. struct ocfs2_dinode *fe = NULL;
  543. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  544. struct ocfs2_dir_lookup_result lookup = { NULL, };
  545. mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
  546. old_dentry->d_name.len, old_dentry->d_name.name,
  547. dentry->d_name.len, dentry->d_name.name);
  548. if (S_ISDIR(inode->i_mode))
  549. return -EPERM;
  550. dquot_initialize(dir);
  551. err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
  552. if (err < 0) {
  553. if (err != -ENOENT)
  554. mlog_errno(err);
  555. return err;
  556. }
  557. if (!dir->i_nlink) {
  558. err = -ENOENT;
  559. goto out;
  560. }
  561. err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  562. dentry->d_name.len);
  563. if (err)
  564. goto out;
  565. err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  566. dentry->d_name.name,
  567. dentry->d_name.len, &lookup);
  568. if (err < 0) {
  569. mlog_errno(err);
  570. goto out;
  571. }
  572. err = ocfs2_inode_lock(inode, &fe_bh, 1);
  573. if (err < 0) {
  574. if (err != -ENOENT)
  575. mlog_errno(err);
  576. goto out;
  577. }
  578. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  579. if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
  580. err = -EMLINK;
  581. goto out_unlock_inode;
  582. }
  583. handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
  584. if (IS_ERR(handle)) {
  585. err = PTR_ERR(handle);
  586. handle = NULL;
  587. mlog_errno(err);
  588. goto out_unlock_inode;
  589. }
  590. err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  591. OCFS2_JOURNAL_ACCESS_WRITE);
  592. if (err < 0) {
  593. mlog_errno(err);
  594. goto out_commit;
  595. }
  596. inc_nlink(inode);
  597. inode->i_ctime = CURRENT_TIME;
  598. ocfs2_set_links_count(fe, inode->i_nlink);
  599. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  600. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  601. err = ocfs2_journal_dirty(handle, fe_bh);
  602. if (err < 0) {
  603. ocfs2_add_links_count(fe, -1);
  604. drop_nlink(inode);
  605. mlog_errno(err);
  606. goto out_commit;
  607. }
  608. err = ocfs2_add_entry(handle, dentry, inode,
  609. OCFS2_I(inode)->ip_blkno,
  610. parent_fe_bh, &lookup);
  611. if (err) {
  612. ocfs2_add_links_count(fe, -1);
  613. drop_nlink(inode);
  614. mlog_errno(err);
  615. goto out_commit;
  616. }
  617. err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  618. if (err) {
  619. mlog_errno(err);
  620. goto out_commit;
  621. }
  622. atomic_inc(&inode->i_count);
  623. dentry->d_op = &ocfs2_dentry_ops;
  624. d_instantiate(dentry, inode);
  625. out_commit:
  626. ocfs2_commit_trans(osb, handle);
  627. out_unlock_inode:
  628. ocfs2_inode_unlock(inode, 1);
  629. out:
  630. ocfs2_inode_unlock(dir, 1);
  631. brelse(fe_bh);
  632. brelse(parent_fe_bh);
  633. ocfs2_free_dir_lookup_result(&lookup);
  634. mlog_exit(err);
  635. return err;
  636. }
  637. /*
  638. * Takes and drops an exclusive lock on the given dentry. This will
  639. * force other nodes to drop it.
  640. */
  641. static int ocfs2_remote_dentry_delete(struct dentry *dentry)
  642. {
  643. int ret;
  644. ret = ocfs2_dentry_lock(dentry, 1);
  645. if (ret)
  646. mlog_errno(ret);
  647. else
  648. ocfs2_dentry_unlock(dentry, 1);
  649. return ret;
  650. }
  651. static inline int inode_is_unlinkable(struct inode *inode)
  652. {
  653. if (S_ISDIR(inode->i_mode)) {
  654. if (inode->i_nlink == 2)
  655. return 1;
  656. return 0;
  657. }
  658. if (inode->i_nlink == 1)
  659. return 1;
  660. return 0;
  661. }
  662. static int ocfs2_unlink(struct inode *dir,
  663. struct dentry *dentry)
  664. {
  665. int status;
  666. int child_locked = 0;
  667. struct inode *inode = dentry->d_inode;
  668. struct inode *orphan_dir = NULL;
  669. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  670. u64 blkno;
  671. struct ocfs2_dinode *fe = NULL;
  672. struct buffer_head *fe_bh = NULL;
  673. struct buffer_head *parent_node_bh = NULL;
  674. handle_t *handle = NULL;
  675. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  676. struct ocfs2_dir_lookup_result lookup = { NULL, };
  677. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  678. mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
  679. dentry->d_name.len, dentry->d_name.name);
  680. dquot_initialize(dir);
  681. BUG_ON(dentry->d_parent->d_inode != dir);
  682. mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  683. if (inode == osb->root_inode) {
  684. mlog(0, "Cannot delete the root directory\n");
  685. return -EPERM;
  686. }
  687. status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
  688. OI_LS_PARENT);
  689. if (status < 0) {
  690. if (status != -ENOENT)
  691. mlog_errno(status);
  692. return status;
  693. }
  694. status = ocfs2_find_files_on_disk(dentry->d_name.name,
  695. dentry->d_name.len, &blkno, dir,
  696. &lookup);
  697. if (status < 0) {
  698. if (status != -ENOENT)
  699. mlog_errno(status);
  700. goto leave;
  701. }
  702. if (OCFS2_I(inode)->ip_blkno != blkno) {
  703. status = -ENOENT;
  704. mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
  705. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  706. (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
  707. goto leave;
  708. }
  709. status = ocfs2_inode_lock(inode, &fe_bh, 1);
  710. if (status < 0) {
  711. if (status != -ENOENT)
  712. mlog_errno(status);
  713. goto leave;
  714. }
  715. child_locked = 1;
  716. if (S_ISDIR(inode->i_mode)) {
  717. if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
  718. status = -ENOTEMPTY;
  719. goto leave;
  720. }
  721. }
  722. status = ocfs2_remote_dentry_delete(dentry);
  723. if (status < 0) {
  724. /* This remote delete should succeed under all normal
  725. * circumstances. */
  726. mlog_errno(status);
  727. goto leave;
  728. }
  729. if (inode_is_unlinkable(inode)) {
  730. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  731. OCFS2_I(inode)->ip_blkno,
  732. orphan_name, &orphan_insert);
  733. if (status < 0) {
  734. mlog_errno(status);
  735. goto leave;
  736. }
  737. }
  738. handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
  739. if (IS_ERR(handle)) {
  740. status = PTR_ERR(handle);
  741. handle = NULL;
  742. mlog_errno(status);
  743. goto leave;
  744. }
  745. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  746. OCFS2_JOURNAL_ACCESS_WRITE);
  747. if (status < 0) {
  748. mlog_errno(status);
  749. goto leave;
  750. }
  751. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  752. if (inode_is_unlinkable(inode)) {
  753. status = ocfs2_orphan_add(osb, handle, inode, fe_bh, orphan_name,
  754. &orphan_insert, orphan_dir);
  755. if (status < 0) {
  756. mlog_errno(status);
  757. goto leave;
  758. }
  759. }
  760. /* delete the name from the parent dir */
  761. status = ocfs2_delete_entry(handle, dir, &lookup);
  762. if (status < 0) {
  763. mlog_errno(status);
  764. goto leave;
  765. }
  766. if (S_ISDIR(inode->i_mode))
  767. drop_nlink(inode);
  768. drop_nlink(inode);
  769. ocfs2_set_links_count(fe, inode->i_nlink);
  770. status = ocfs2_journal_dirty(handle, fe_bh);
  771. if (status < 0) {
  772. mlog_errno(status);
  773. goto leave;
  774. }
  775. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  776. if (S_ISDIR(inode->i_mode))
  777. drop_nlink(dir);
  778. status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
  779. if (status < 0) {
  780. mlog_errno(status);
  781. if (S_ISDIR(inode->i_mode))
  782. inc_nlink(dir);
  783. }
  784. leave:
  785. if (handle)
  786. ocfs2_commit_trans(osb, handle);
  787. if (child_locked)
  788. ocfs2_inode_unlock(inode, 1);
  789. ocfs2_inode_unlock(dir, 1);
  790. if (orphan_dir) {
  791. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  792. ocfs2_inode_unlock(orphan_dir, 1);
  793. mutex_unlock(&orphan_dir->i_mutex);
  794. iput(orphan_dir);
  795. }
  796. brelse(fe_bh);
  797. brelse(parent_node_bh);
  798. ocfs2_free_dir_lookup_result(&orphan_insert);
  799. ocfs2_free_dir_lookup_result(&lookup);
  800. mlog_exit(status);
  801. return status;
  802. }
  803. /*
  804. * The only place this should be used is rename!
  805. * if they have the same id, then the 1st one is the only one locked.
  806. */
  807. static int ocfs2_double_lock(struct ocfs2_super *osb,
  808. struct buffer_head **bh1,
  809. struct inode *inode1,
  810. struct buffer_head **bh2,
  811. struct inode *inode2)
  812. {
  813. int status;
  814. struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
  815. struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
  816. struct buffer_head **tmpbh;
  817. struct inode *tmpinode;
  818. mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
  819. (unsigned long long)oi1->ip_blkno,
  820. (unsigned long long)oi2->ip_blkno);
  821. if (*bh1)
  822. *bh1 = NULL;
  823. if (*bh2)
  824. *bh2 = NULL;
  825. /* we always want to lock the one with the lower lockid first. */
  826. if (oi1->ip_blkno != oi2->ip_blkno) {
  827. if (oi1->ip_blkno < oi2->ip_blkno) {
  828. /* switch id1 and id2 around */
  829. mlog(0, "switching them around...\n");
  830. tmpbh = bh2;
  831. bh2 = bh1;
  832. bh1 = tmpbh;
  833. tmpinode = inode2;
  834. inode2 = inode1;
  835. inode1 = tmpinode;
  836. }
  837. /* lock id2 */
  838. status = ocfs2_inode_lock_nested(inode2, bh2, 1,
  839. OI_LS_RENAME1);
  840. if (status < 0) {
  841. if (status != -ENOENT)
  842. mlog_errno(status);
  843. goto bail;
  844. }
  845. }
  846. /* lock id1 */
  847. status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
  848. if (status < 0) {
  849. /*
  850. * An error return must mean that no cluster locks
  851. * were held on function exit.
  852. */
  853. if (oi1->ip_blkno != oi2->ip_blkno)
  854. ocfs2_inode_unlock(inode2, 1);
  855. if (status != -ENOENT)
  856. mlog_errno(status);
  857. }
  858. bail:
  859. mlog_exit(status);
  860. return status;
  861. }
  862. static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
  863. {
  864. ocfs2_inode_unlock(inode1, 1);
  865. if (inode1 != inode2)
  866. ocfs2_inode_unlock(inode2, 1);
  867. }
  868. static int ocfs2_rename(struct inode *old_dir,
  869. struct dentry *old_dentry,
  870. struct inode *new_dir,
  871. struct dentry *new_dentry)
  872. {
  873. int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
  874. int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
  875. struct inode *old_inode = old_dentry->d_inode;
  876. struct inode *new_inode = new_dentry->d_inode;
  877. struct inode *orphan_dir = NULL;
  878. struct ocfs2_dinode *newfe = NULL;
  879. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  880. struct buffer_head *newfe_bh = NULL;
  881. struct buffer_head *old_inode_bh = NULL;
  882. struct ocfs2_super *osb = NULL;
  883. u64 newfe_blkno, old_de_ino;
  884. handle_t *handle = NULL;
  885. struct buffer_head *old_dir_bh = NULL;
  886. struct buffer_head *new_dir_bh = NULL;
  887. nlink_t old_dir_nlink = old_dir->i_nlink;
  888. struct ocfs2_dinode *old_di;
  889. struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
  890. struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
  891. struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
  892. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  893. struct ocfs2_dir_lookup_result target_insert = { NULL, };
  894. /* At some point it might be nice to break this function up a
  895. * bit. */
  896. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
  897. old_dir, old_dentry, new_dir, new_dentry,
  898. old_dentry->d_name.len, old_dentry->d_name.name,
  899. new_dentry->d_name.len, new_dentry->d_name.name);
  900. dquot_initialize(old_dir);
  901. dquot_initialize(new_dir);
  902. osb = OCFS2_SB(old_dir->i_sb);
  903. if (new_inode) {
  904. if (!igrab(new_inode))
  905. BUG();
  906. }
  907. /* Assume a directory hierarchy thusly:
  908. * a/b/c
  909. * a/d
  910. * a,b,c, and d are all directories.
  911. *
  912. * from cwd of 'a' on both nodes:
  913. * node1: mv b/c d
  914. * node2: mv d b/c
  915. *
  916. * And that's why, just like the VFS, we need a file system
  917. * rename lock. */
  918. if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
  919. status = ocfs2_rename_lock(osb);
  920. if (status < 0) {
  921. mlog_errno(status);
  922. goto bail;
  923. }
  924. rename_lock = 1;
  925. }
  926. /* if old and new are the same, this'll just do one lock. */
  927. status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
  928. &new_dir_bh, new_dir);
  929. if (status < 0) {
  930. mlog_errno(status);
  931. goto bail;
  932. }
  933. parents_locked = 1;
  934. /* make sure both dirs have bhs
  935. * get an extra ref on old_dir_bh if old==new */
  936. if (!new_dir_bh) {
  937. if (old_dir_bh) {
  938. new_dir_bh = old_dir_bh;
  939. get_bh(new_dir_bh);
  940. } else {
  941. mlog(ML_ERROR, "no old_dir_bh!\n");
  942. status = -EIO;
  943. goto bail;
  944. }
  945. }
  946. /*
  947. * Aside from allowing a meta data update, the locking here
  948. * also ensures that the downconvert thread on other nodes
  949. * won't have to concurrently downconvert the inode and the
  950. * dentry locks.
  951. */
  952. status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
  953. OI_LS_PARENT);
  954. if (status < 0) {
  955. if (status != -ENOENT)
  956. mlog_errno(status);
  957. goto bail;
  958. }
  959. old_child_locked = 1;
  960. status = ocfs2_remote_dentry_delete(old_dentry);
  961. if (status < 0) {
  962. mlog_errno(status);
  963. goto bail;
  964. }
  965. if (S_ISDIR(old_inode->i_mode)) {
  966. u64 old_inode_parent;
  967. update_dot_dot = 1;
  968. status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
  969. old_inode,
  970. &old_inode_dot_dot_res);
  971. if (status) {
  972. status = -EIO;
  973. goto bail;
  974. }
  975. if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
  976. status = -EIO;
  977. goto bail;
  978. }
  979. if (!new_inode && new_dir != old_dir &&
  980. new_dir->i_nlink >= ocfs2_link_max(osb)) {
  981. status = -EMLINK;
  982. goto bail;
  983. }
  984. }
  985. status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
  986. old_dentry->d_name.len,
  987. &old_de_ino);
  988. if (status) {
  989. status = -ENOENT;
  990. goto bail;
  991. }
  992. /*
  993. * Check for inode number is _not_ due to possible IO errors.
  994. * We might rmdir the source, keep it as pwd of some process
  995. * and merrily kill the link to whatever was created under the
  996. * same name. Goodbye sticky bit ;-<
  997. */
  998. if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
  999. status = -ENOENT;
  1000. goto bail;
  1001. }
  1002. /* check if the target already exists (in which case we need
  1003. * to delete it */
  1004. status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
  1005. new_dentry->d_name.len,
  1006. &newfe_blkno, new_dir,
  1007. &target_lookup_res);
  1008. /* The only error we allow here is -ENOENT because the new
  1009. * file not existing is perfectly valid. */
  1010. if ((status < 0) && (status != -ENOENT)) {
  1011. /* If we cannot find the file specified we should just */
  1012. /* return the error... */
  1013. mlog_errno(status);
  1014. goto bail;
  1015. }
  1016. if (status == 0)
  1017. target_exists = 1;
  1018. if (!target_exists && new_inode) {
  1019. /*
  1020. * Target was unlinked by another node while we were
  1021. * waiting to get to ocfs2_rename(). There isn't
  1022. * anything we can do here to help the situation, so
  1023. * bubble up the appropriate error.
  1024. */
  1025. status = -ENOENT;
  1026. goto bail;
  1027. }
  1028. /* In case we need to overwrite an existing file, we blow it
  1029. * away first */
  1030. if (target_exists) {
  1031. /* VFS didn't think there existed an inode here, but
  1032. * someone else in the cluster must have raced our
  1033. * rename to create one. Today we error cleanly, in
  1034. * the future we should consider calling iget to build
  1035. * a new struct inode for this entry. */
  1036. if (!new_inode) {
  1037. status = -EACCES;
  1038. mlog(0, "We found an inode for name %.*s but VFS "
  1039. "didn't give us one.\n", new_dentry->d_name.len,
  1040. new_dentry->d_name.name);
  1041. goto bail;
  1042. }
  1043. if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
  1044. status = -EACCES;
  1045. mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
  1046. (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
  1047. (unsigned long long)newfe_blkno,
  1048. OCFS2_I(new_inode)->ip_flags);
  1049. goto bail;
  1050. }
  1051. status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
  1052. if (status < 0) {
  1053. if (status != -ENOENT)
  1054. mlog_errno(status);
  1055. goto bail;
  1056. }
  1057. new_child_locked = 1;
  1058. status = ocfs2_remote_dentry_delete(new_dentry);
  1059. if (status < 0) {
  1060. mlog_errno(status);
  1061. goto bail;
  1062. }
  1063. newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
  1064. mlog(0, "aha rename over existing... new_blkno=%llu "
  1065. "newfebh=%p bhblocknr=%llu\n",
  1066. (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
  1067. (unsigned long long)newfe_bh->b_blocknr : 0ULL);
  1068. if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
  1069. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1070. OCFS2_I(new_inode)->ip_blkno,
  1071. orphan_name, &orphan_insert);
  1072. if (status < 0) {
  1073. mlog_errno(status);
  1074. goto bail;
  1075. }
  1076. }
  1077. } else {
  1078. BUG_ON(new_dentry->d_parent->d_inode != new_dir);
  1079. status = ocfs2_check_dir_for_entry(new_dir,
  1080. new_dentry->d_name.name,
  1081. new_dentry->d_name.len);
  1082. if (status)
  1083. goto bail;
  1084. status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
  1085. new_dentry->d_name.name,
  1086. new_dentry->d_name.len,
  1087. &target_insert);
  1088. if (status < 0) {
  1089. mlog_errno(status);
  1090. goto bail;
  1091. }
  1092. }
  1093. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1094. if (IS_ERR(handle)) {
  1095. status = PTR_ERR(handle);
  1096. handle = NULL;
  1097. mlog_errno(status);
  1098. goto bail;
  1099. }
  1100. if (target_exists) {
  1101. if (S_ISDIR(new_inode->i_mode)) {
  1102. if (new_inode->i_nlink != 2 ||
  1103. !ocfs2_empty_dir(new_inode)) {
  1104. status = -ENOTEMPTY;
  1105. goto bail;
  1106. }
  1107. }
  1108. status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
  1109. newfe_bh,
  1110. OCFS2_JOURNAL_ACCESS_WRITE);
  1111. if (status < 0) {
  1112. mlog_errno(status);
  1113. goto bail;
  1114. }
  1115. if (S_ISDIR(new_inode->i_mode) ||
  1116. (ocfs2_read_links_count(newfe) == 1)) {
  1117. status = ocfs2_orphan_add(osb, handle, new_inode,
  1118. newfe_bh, orphan_name,
  1119. &orphan_insert, orphan_dir);
  1120. if (status < 0) {
  1121. mlog_errno(status);
  1122. goto bail;
  1123. }
  1124. }
  1125. /* change the dirent to point to the correct inode */
  1126. status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
  1127. old_inode);
  1128. if (status < 0) {
  1129. mlog_errno(status);
  1130. goto bail;
  1131. }
  1132. new_dir->i_version++;
  1133. if (S_ISDIR(new_inode->i_mode))
  1134. ocfs2_set_links_count(newfe, 0);
  1135. else
  1136. ocfs2_add_links_count(newfe, -1);
  1137. status = ocfs2_journal_dirty(handle, newfe_bh);
  1138. if (status < 0) {
  1139. mlog_errno(status);
  1140. goto bail;
  1141. }
  1142. } else {
  1143. /* if the name was not found in new_dir, add it now */
  1144. status = ocfs2_add_entry(handle, new_dentry, old_inode,
  1145. OCFS2_I(old_inode)->ip_blkno,
  1146. new_dir_bh, &target_insert);
  1147. }
  1148. old_inode->i_ctime = CURRENT_TIME;
  1149. mark_inode_dirty(old_inode);
  1150. status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
  1151. old_inode_bh,
  1152. OCFS2_JOURNAL_ACCESS_WRITE);
  1153. if (status >= 0) {
  1154. old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
  1155. old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
  1156. old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
  1157. status = ocfs2_journal_dirty(handle, old_inode_bh);
  1158. if (status < 0)
  1159. mlog_errno(status);
  1160. } else
  1161. mlog_errno(status);
  1162. /*
  1163. * Now that the name has been added to new_dir, remove the old name.
  1164. *
  1165. * We don't keep any directory entry context around until now
  1166. * because the insert might have changed the type of directory
  1167. * we're dealing with.
  1168. */
  1169. status = ocfs2_find_entry(old_dentry->d_name.name,
  1170. old_dentry->d_name.len, old_dir,
  1171. &old_entry_lookup);
  1172. if (status)
  1173. goto bail;
  1174. status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
  1175. if (status < 0) {
  1176. mlog_errno(status);
  1177. goto bail;
  1178. }
  1179. if (new_inode) {
  1180. new_inode->i_nlink--;
  1181. new_inode->i_ctime = CURRENT_TIME;
  1182. }
  1183. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
  1184. if (update_dot_dot) {
  1185. status = ocfs2_update_entry(old_inode, handle,
  1186. &old_inode_dot_dot_res, new_dir);
  1187. old_dir->i_nlink--;
  1188. if (new_inode) {
  1189. new_inode->i_nlink--;
  1190. } else {
  1191. inc_nlink(new_dir);
  1192. mark_inode_dirty(new_dir);
  1193. }
  1194. }
  1195. mark_inode_dirty(old_dir);
  1196. ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
  1197. if (new_inode) {
  1198. mark_inode_dirty(new_inode);
  1199. ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
  1200. }
  1201. if (old_dir != new_dir) {
  1202. /* Keep the same times on both directories.*/
  1203. new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
  1204. /*
  1205. * This will also pick up the i_nlink change from the
  1206. * block above.
  1207. */
  1208. ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
  1209. }
  1210. if (old_dir_nlink != old_dir->i_nlink) {
  1211. if (!old_dir_bh) {
  1212. mlog(ML_ERROR, "need to change nlink for old dir "
  1213. "%llu from %d to %d but bh is NULL!\n",
  1214. (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
  1215. (int)old_dir_nlink, old_dir->i_nlink);
  1216. } else {
  1217. struct ocfs2_dinode *fe;
  1218. status = ocfs2_journal_access_di(handle,
  1219. INODE_CACHE(old_dir),
  1220. old_dir_bh,
  1221. OCFS2_JOURNAL_ACCESS_WRITE);
  1222. fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
  1223. ocfs2_set_links_count(fe, old_dir->i_nlink);
  1224. status = ocfs2_journal_dirty(handle, old_dir_bh);
  1225. }
  1226. }
  1227. ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
  1228. status = 0;
  1229. bail:
  1230. if (rename_lock)
  1231. ocfs2_rename_unlock(osb);
  1232. if (handle)
  1233. ocfs2_commit_trans(osb, handle);
  1234. if (parents_locked)
  1235. ocfs2_double_unlock(old_dir, new_dir);
  1236. if (old_child_locked)
  1237. ocfs2_inode_unlock(old_inode, 1);
  1238. if (new_child_locked)
  1239. ocfs2_inode_unlock(new_inode, 1);
  1240. if (orphan_dir) {
  1241. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1242. ocfs2_inode_unlock(orphan_dir, 1);
  1243. mutex_unlock(&orphan_dir->i_mutex);
  1244. iput(orphan_dir);
  1245. }
  1246. if (new_inode)
  1247. sync_mapping_buffers(old_inode->i_mapping);
  1248. if (new_inode)
  1249. iput(new_inode);
  1250. ocfs2_free_dir_lookup_result(&target_lookup_res);
  1251. ocfs2_free_dir_lookup_result(&old_entry_lookup);
  1252. ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
  1253. ocfs2_free_dir_lookup_result(&orphan_insert);
  1254. ocfs2_free_dir_lookup_result(&target_insert);
  1255. brelse(newfe_bh);
  1256. brelse(old_inode_bh);
  1257. brelse(old_dir_bh);
  1258. brelse(new_dir_bh);
  1259. mlog_exit(status);
  1260. return status;
  1261. }
  1262. /*
  1263. * we expect i_size = strlen(symname). Copy symname into the file
  1264. * data, including the null terminator.
  1265. */
  1266. static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
  1267. handle_t *handle,
  1268. struct inode *inode,
  1269. const char *symname)
  1270. {
  1271. struct buffer_head **bhs = NULL;
  1272. const char *c;
  1273. struct super_block *sb = osb->sb;
  1274. u64 p_blkno, p_blocks;
  1275. int virtual, blocks, status, i, bytes_left;
  1276. bytes_left = i_size_read(inode) + 1;
  1277. /* we can't trust i_blocks because we're actually going to
  1278. * write i_size + 1 bytes. */
  1279. blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  1280. mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
  1281. (unsigned long long)inode->i_blocks,
  1282. i_size_read(inode), blocks);
  1283. /* Sanity check -- make sure we're going to fit. */
  1284. if (bytes_left >
  1285. ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
  1286. status = -EIO;
  1287. mlog_errno(status);
  1288. goto bail;
  1289. }
  1290. bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
  1291. if (!bhs) {
  1292. status = -ENOMEM;
  1293. mlog_errno(status);
  1294. goto bail;
  1295. }
  1296. status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
  1297. NULL);
  1298. if (status < 0) {
  1299. mlog_errno(status);
  1300. goto bail;
  1301. }
  1302. /* links can never be larger than one cluster so we know this
  1303. * is all going to be contiguous, but do a sanity check
  1304. * anyway. */
  1305. if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
  1306. status = -EIO;
  1307. mlog_errno(status);
  1308. goto bail;
  1309. }
  1310. virtual = 0;
  1311. while(bytes_left > 0) {
  1312. c = &symname[virtual * sb->s_blocksize];
  1313. bhs[virtual] = sb_getblk(sb, p_blkno);
  1314. if (!bhs[virtual]) {
  1315. status = -ENOMEM;
  1316. mlog_errno(status);
  1317. goto bail;
  1318. }
  1319. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
  1320. bhs[virtual]);
  1321. status = ocfs2_journal_access(handle, INODE_CACHE(inode),
  1322. bhs[virtual],
  1323. OCFS2_JOURNAL_ACCESS_CREATE);
  1324. if (status < 0) {
  1325. mlog_errno(status);
  1326. goto bail;
  1327. }
  1328. memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
  1329. memcpy(bhs[virtual]->b_data, c,
  1330. (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
  1331. bytes_left);
  1332. status = ocfs2_journal_dirty(handle, bhs[virtual]);
  1333. if (status < 0) {
  1334. mlog_errno(status);
  1335. goto bail;
  1336. }
  1337. virtual++;
  1338. p_blkno++;
  1339. bytes_left -= sb->s_blocksize;
  1340. }
  1341. status = 0;
  1342. bail:
  1343. if (bhs) {
  1344. for(i = 0; i < blocks; i++)
  1345. brelse(bhs[i]);
  1346. kfree(bhs);
  1347. }
  1348. mlog_exit(status);
  1349. return status;
  1350. }
  1351. static int ocfs2_symlink(struct inode *dir,
  1352. struct dentry *dentry,
  1353. const char *symname)
  1354. {
  1355. int status, l, credits;
  1356. u64 newsize;
  1357. struct ocfs2_super *osb = NULL;
  1358. struct inode *inode = NULL;
  1359. struct super_block *sb;
  1360. struct buffer_head *new_fe_bh = NULL;
  1361. struct buffer_head *parent_fe_bh = NULL;
  1362. struct ocfs2_dinode *fe = NULL;
  1363. struct ocfs2_dinode *dirfe;
  1364. handle_t *handle = NULL;
  1365. struct ocfs2_alloc_context *inode_ac = NULL;
  1366. struct ocfs2_alloc_context *data_ac = NULL;
  1367. struct ocfs2_alloc_context *xattr_ac = NULL;
  1368. int want_clusters = 0;
  1369. int xattr_credits = 0;
  1370. struct ocfs2_security_xattr_info si = {
  1371. .enable = 1,
  1372. };
  1373. int did_quota = 0, did_quota_inode = 0;
  1374. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1375. mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
  1376. dentry, symname, dentry->d_name.len, dentry->d_name.name);
  1377. dquot_initialize(dir);
  1378. sb = dir->i_sb;
  1379. osb = OCFS2_SB(sb);
  1380. l = strlen(symname) + 1;
  1381. credits = ocfs2_calc_symlink_credits(sb);
  1382. /* lock the parent directory */
  1383. status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
  1384. if (status < 0) {
  1385. if (status != -ENOENT)
  1386. mlog_errno(status);
  1387. return status;
  1388. }
  1389. dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1390. if (!ocfs2_read_links_count(dirfe)) {
  1391. /* can't make a file in a deleted directory. */
  1392. status = -ENOENT;
  1393. goto bail;
  1394. }
  1395. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1396. dentry->d_name.len);
  1397. if (status)
  1398. goto bail;
  1399. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
  1400. dentry->d_name.name,
  1401. dentry->d_name.len, &lookup);
  1402. if (status < 0) {
  1403. mlog_errno(status);
  1404. goto bail;
  1405. }
  1406. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1407. if (status < 0) {
  1408. if (status != -ENOSPC)
  1409. mlog_errno(status);
  1410. goto bail;
  1411. }
  1412. inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
  1413. if (!inode) {
  1414. status = -ENOMEM;
  1415. mlog_errno(status);
  1416. goto bail;
  1417. }
  1418. /* get security xattr */
  1419. status = ocfs2_init_security_get(inode, dir, &si);
  1420. if (status) {
  1421. if (status == -EOPNOTSUPP)
  1422. si.enable = 0;
  1423. else {
  1424. mlog_errno(status);
  1425. goto bail;
  1426. }
  1427. }
  1428. /* calculate meta data/clusters for setting security xattr */
  1429. if (si.enable) {
  1430. status = ocfs2_calc_security_init(dir, &si, &want_clusters,
  1431. &xattr_credits, &xattr_ac);
  1432. if (status < 0) {
  1433. mlog_errno(status);
  1434. goto bail;
  1435. }
  1436. }
  1437. /* don't reserve bitmap space for fast symlinks. */
  1438. if (l > ocfs2_fast_symlink_chars(sb))
  1439. want_clusters += 1;
  1440. status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
  1441. if (status < 0) {
  1442. if (status != -ENOSPC)
  1443. mlog_errno(status);
  1444. goto bail;
  1445. }
  1446. handle = ocfs2_start_trans(osb, credits + xattr_credits);
  1447. if (IS_ERR(handle)) {
  1448. status = PTR_ERR(handle);
  1449. handle = NULL;
  1450. mlog_errno(status);
  1451. goto bail;
  1452. }
  1453. status = dquot_alloc_inode(inode);
  1454. if (status)
  1455. goto bail;
  1456. did_quota_inode = 1;
  1457. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry,
  1458. inode->i_mode, dentry->d_name.len,
  1459. dentry->d_name.name);
  1460. status = ocfs2_mknod_locked(osb, dir, inode,
  1461. 0, &new_fe_bh, parent_fe_bh, handle,
  1462. inode_ac);
  1463. if (status < 0) {
  1464. mlog_errno(status);
  1465. goto bail;
  1466. }
  1467. fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
  1468. inode->i_rdev = 0;
  1469. newsize = l - 1;
  1470. if (l > ocfs2_fast_symlink_chars(sb)) {
  1471. u32 offset = 0;
  1472. inode->i_op = &ocfs2_symlink_inode_operations;
  1473. status = dquot_alloc_space_nodirty(inode,
  1474. ocfs2_clusters_to_bytes(osb->sb, 1));
  1475. if (status)
  1476. goto bail;
  1477. did_quota = 1;
  1478. status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
  1479. new_fe_bh,
  1480. handle, data_ac, NULL,
  1481. NULL);
  1482. if (status < 0) {
  1483. if (status != -ENOSPC && status != -EINTR) {
  1484. mlog(ML_ERROR,
  1485. "Failed to extend file to %llu\n",
  1486. (unsigned long long)newsize);
  1487. mlog_errno(status);
  1488. status = -ENOSPC;
  1489. }
  1490. goto bail;
  1491. }
  1492. i_size_write(inode, newsize);
  1493. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1494. } else {
  1495. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  1496. memcpy((char *) fe->id2.i_symlink, symname, l);
  1497. i_size_write(inode, newsize);
  1498. inode->i_blocks = 0;
  1499. }
  1500. status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
  1501. if (status < 0) {
  1502. mlog_errno(status);
  1503. goto bail;
  1504. }
  1505. if (!ocfs2_inode_is_fast_symlink(inode)) {
  1506. status = ocfs2_create_symlink_data(osb, handle, inode,
  1507. symname);
  1508. if (status < 0) {
  1509. mlog_errno(status);
  1510. goto bail;
  1511. }
  1512. }
  1513. if (si.enable) {
  1514. status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
  1515. xattr_ac, data_ac);
  1516. if (status < 0) {
  1517. mlog_errno(status);
  1518. goto bail;
  1519. }
  1520. }
  1521. status = ocfs2_add_entry(handle, dentry, inode,
  1522. le64_to_cpu(fe->i_blkno), parent_fe_bh,
  1523. &lookup);
  1524. if (status < 0) {
  1525. mlog_errno(status);
  1526. goto bail;
  1527. }
  1528. status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
  1529. if (status) {
  1530. mlog_errno(status);
  1531. goto bail;
  1532. }
  1533. insert_inode_hash(inode);
  1534. dentry->d_op = &ocfs2_dentry_ops;
  1535. d_instantiate(dentry, inode);
  1536. bail:
  1537. if (status < 0 && did_quota)
  1538. dquot_free_space_nodirty(inode,
  1539. ocfs2_clusters_to_bytes(osb->sb, 1));
  1540. if (status < 0 && did_quota_inode)
  1541. dquot_free_inode(inode);
  1542. if (handle)
  1543. ocfs2_commit_trans(osb, handle);
  1544. ocfs2_inode_unlock(dir, 1);
  1545. brelse(new_fe_bh);
  1546. brelse(parent_fe_bh);
  1547. kfree(si.name);
  1548. kfree(si.value);
  1549. ocfs2_free_dir_lookup_result(&lookup);
  1550. if (inode_ac)
  1551. ocfs2_free_alloc_context(inode_ac);
  1552. if (data_ac)
  1553. ocfs2_free_alloc_context(data_ac);
  1554. if (xattr_ac)
  1555. ocfs2_free_alloc_context(xattr_ac);
  1556. if ((status < 0) && inode) {
  1557. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
  1558. clear_nlink(inode);
  1559. iput(inode);
  1560. }
  1561. mlog_exit(status);
  1562. return status;
  1563. }
  1564. static int ocfs2_blkno_stringify(u64 blkno, char *name)
  1565. {
  1566. int status, namelen;
  1567. mlog_entry_void();
  1568. namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
  1569. (long long)blkno);
  1570. if (namelen <= 0) {
  1571. if (namelen)
  1572. status = namelen;
  1573. else
  1574. status = -EINVAL;
  1575. mlog_errno(status);
  1576. goto bail;
  1577. }
  1578. if (namelen != OCFS2_ORPHAN_NAMELEN) {
  1579. status = -EINVAL;
  1580. mlog_errno(status);
  1581. goto bail;
  1582. }
  1583. mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
  1584. namelen);
  1585. status = 0;
  1586. bail:
  1587. mlog_exit(status);
  1588. return status;
  1589. }
  1590. static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
  1591. struct inode **ret_orphan_dir,
  1592. u64 blkno,
  1593. char *name,
  1594. struct ocfs2_dir_lookup_result *lookup)
  1595. {
  1596. struct inode *orphan_dir_inode;
  1597. struct buffer_head *orphan_dir_bh = NULL;
  1598. int status = 0;
  1599. status = ocfs2_blkno_stringify(blkno, name);
  1600. if (status < 0) {
  1601. mlog_errno(status);
  1602. return status;
  1603. }
  1604. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1605. ORPHAN_DIR_SYSTEM_INODE,
  1606. osb->slot_num);
  1607. if (!orphan_dir_inode) {
  1608. status = -ENOENT;
  1609. mlog_errno(status);
  1610. return status;
  1611. }
  1612. mutex_lock(&orphan_dir_inode->i_mutex);
  1613. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1614. if (status < 0) {
  1615. mlog_errno(status);
  1616. goto leave;
  1617. }
  1618. status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
  1619. orphan_dir_bh, name,
  1620. OCFS2_ORPHAN_NAMELEN, lookup);
  1621. if (status < 0) {
  1622. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1623. mlog_errno(status);
  1624. goto leave;
  1625. }
  1626. *ret_orphan_dir = orphan_dir_inode;
  1627. leave:
  1628. if (status) {
  1629. mutex_unlock(&orphan_dir_inode->i_mutex);
  1630. iput(orphan_dir_inode);
  1631. }
  1632. brelse(orphan_dir_bh);
  1633. mlog_exit(status);
  1634. return status;
  1635. }
  1636. static int ocfs2_orphan_add(struct ocfs2_super *osb,
  1637. handle_t *handle,
  1638. struct inode *inode,
  1639. struct buffer_head *fe_bh,
  1640. char *name,
  1641. struct ocfs2_dir_lookup_result *lookup,
  1642. struct inode *orphan_dir_inode)
  1643. {
  1644. struct buffer_head *orphan_dir_bh = NULL;
  1645. int status = 0;
  1646. struct ocfs2_dinode *orphan_fe;
  1647. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  1648. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  1649. status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
  1650. if (status < 0) {
  1651. mlog_errno(status);
  1652. goto leave;
  1653. }
  1654. status = ocfs2_journal_access_di(handle,
  1655. INODE_CACHE(orphan_dir_inode),
  1656. orphan_dir_bh,
  1657. OCFS2_JOURNAL_ACCESS_WRITE);
  1658. if (status < 0) {
  1659. mlog_errno(status);
  1660. goto leave;
  1661. }
  1662. /* we're a cluster, and nlink can change on disk from
  1663. * underneath us... */
  1664. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1665. if (S_ISDIR(inode->i_mode))
  1666. ocfs2_add_links_count(orphan_fe, 1);
  1667. orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
  1668. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1669. if (status < 0) {
  1670. mlog_errno(status);
  1671. goto leave;
  1672. }
  1673. status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
  1674. OCFS2_ORPHAN_NAMELEN, inode,
  1675. OCFS2_I(inode)->ip_blkno,
  1676. orphan_dir_bh, lookup);
  1677. if (status < 0) {
  1678. mlog_errno(status);
  1679. goto leave;
  1680. }
  1681. /*
  1682. * We're going to journal the change of i_flags and i_orphaned_slot.
  1683. * It's safe anyway, though some callers may duplicate the journaling.
  1684. * Journaling within the func just make the logic look more
  1685. * straightforward.
  1686. */
  1687. status = ocfs2_journal_access_di(handle,
  1688. INODE_CACHE(inode),
  1689. fe_bh,
  1690. OCFS2_JOURNAL_ACCESS_WRITE);
  1691. if (status < 0) {
  1692. mlog_errno(status);
  1693. goto leave;
  1694. }
  1695. le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
  1696. OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
  1697. /* Record which orphan dir our inode now resides
  1698. * in. delete_inode will use this to determine which orphan
  1699. * dir to lock. */
  1700. fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
  1701. ocfs2_journal_dirty(handle, fe_bh);
  1702. mlog(0, "Inode %llu orphaned in slot %d\n",
  1703. (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
  1704. leave:
  1705. brelse(orphan_dir_bh);
  1706. mlog_exit(status);
  1707. return status;
  1708. }
  1709. /* unlike orphan_add, we expect the orphan dir to already be locked here. */
  1710. int ocfs2_orphan_del(struct ocfs2_super *osb,
  1711. handle_t *handle,
  1712. struct inode *orphan_dir_inode,
  1713. struct inode *inode,
  1714. struct buffer_head *orphan_dir_bh)
  1715. {
  1716. char name[OCFS2_ORPHAN_NAMELEN + 1];
  1717. struct ocfs2_dinode *orphan_fe;
  1718. int status = 0;
  1719. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1720. mlog_entry_void();
  1721. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
  1722. if (status < 0) {
  1723. mlog_errno(status);
  1724. goto leave;
  1725. }
  1726. mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
  1727. name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
  1728. OCFS2_ORPHAN_NAMELEN);
  1729. /* find it's spot in the orphan directory */
  1730. status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
  1731. &lookup);
  1732. if (status) {
  1733. mlog_errno(status);
  1734. goto leave;
  1735. }
  1736. /* remove it from the orphan directory */
  1737. status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
  1738. if (status < 0) {
  1739. mlog_errno(status);
  1740. goto leave;
  1741. }
  1742. status = ocfs2_journal_access_di(handle,
  1743. INODE_CACHE(orphan_dir_inode),
  1744. orphan_dir_bh,
  1745. OCFS2_JOURNAL_ACCESS_WRITE);
  1746. if (status < 0) {
  1747. mlog_errno(status);
  1748. goto leave;
  1749. }
  1750. /* do the i_nlink dance! :) */
  1751. orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
  1752. if (S_ISDIR(inode->i_mode))
  1753. ocfs2_add_links_count(orphan_fe, -1);
  1754. orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
  1755. status = ocfs2_journal_dirty(handle, orphan_dir_bh);
  1756. if (status < 0) {
  1757. mlog_errno(status);
  1758. goto leave;
  1759. }
  1760. leave:
  1761. ocfs2_free_dir_lookup_result(&lookup);
  1762. mlog_exit(status);
  1763. return status;
  1764. }
  1765. int ocfs2_create_inode_in_orphan(struct inode *dir,
  1766. int mode,
  1767. struct inode **new_inode)
  1768. {
  1769. int status, did_quota_inode = 0;
  1770. struct inode *inode = NULL;
  1771. struct inode *orphan_dir = NULL;
  1772. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1773. struct ocfs2_dinode *di = NULL;
  1774. handle_t *handle = NULL;
  1775. char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
  1776. struct buffer_head *parent_di_bh = NULL;
  1777. struct buffer_head *new_di_bh = NULL;
  1778. struct ocfs2_alloc_context *inode_ac = NULL;
  1779. struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
  1780. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1781. if (status < 0) {
  1782. if (status != -ENOENT)
  1783. mlog_errno(status);
  1784. return status;
  1785. }
  1786. /*
  1787. * We give the orphan dir the root blkno to fake an orphan name,
  1788. * and allocate enough space for our insertion.
  1789. */
  1790. status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
  1791. osb->root_blkno,
  1792. orphan_name, &orphan_insert);
  1793. if (status < 0) {
  1794. mlog_errno(status);
  1795. goto leave;
  1796. }
  1797. /* reserve an inode spot */
  1798. status = ocfs2_reserve_new_inode(osb, &inode_ac);
  1799. if (status < 0) {
  1800. if (status != -ENOSPC)
  1801. mlog_errno(status);
  1802. goto leave;
  1803. }
  1804. inode = ocfs2_get_init_inode(dir, mode);
  1805. if (!inode) {
  1806. status = -ENOMEM;
  1807. mlog_errno(status);
  1808. goto leave;
  1809. }
  1810. handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
  1811. if (IS_ERR(handle)) {
  1812. status = PTR_ERR(handle);
  1813. handle = NULL;
  1814. mlog_errno(status);
  1815. goto leave;
  1816. }
  1817. status = dquot_alloc_inode(inode);
  1818. if (status)
  1819. goto leave;
  1820. did_quota_inode = 1;
  1821. inode->i_nlink = 0;
  1822. /* do the real work now. */
  1823. status = ocfs2_mknod_locked(osb, dir, inode,
  1824. 0, &new_di_bh, parent_di_bh, handle,
  1825. inode_ac);
  1826. if (status < 0) {
  1827. mlog_errno(status);
  1828. goto leave;
  1829. }
  1830. status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, orphan_name);
  1831. if (status < 0) {
  1832. mlog_errno(status);
  1833. goto leave;
  1834. }
  1835. di = (struct ocfs2_dinode *)new_di_bh->b_data;
  1836. status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
  1837. &orphan_insert, orphan_dir);
  1838. if (status < 0) {
  1839. mlog_errno(status);
  1840. goto leave;
  1841. }
  1842. /* get open lock so that only nodes can't remove it from orphan dir. */
  1843. status = ocfs2_open_lock(inode);
  1844. if (status < 0)
  1845. mlog_errno(status);
  1846. insert_inode_hash(inode);
  1847. leave:
  1848. if (status < 0 && did_quota_inode)
  1849. dquot_free_inode(inode);
  1850. if (handle)
  1851. ocfs2_commit_trans(osb, handle);
  1852. if (orphan_dir) {
  1853. /* This was locked for us in ocfs2_prepare_orphan_dir() */
  1854. ocfs2_inode_unlock(orphan_dir, 1);
  1855. mutex_unlock(&orphan_dir->i_mutex);
  1856. iput(orphan_dir);
  1857. }
  1858. if (status == -ENOSPC)
  1859. mlog(0, "Disk is full\n");
  1860. if ((status < 0) && inode) {
  1861. clear_nlink(inode);
  1862. iput(inode);
  1863. }
  1864. if (inode_ac)
  1865. ocfs2_free_alloc_context(inode_ac);
  1866. brelse(new_di_bh);
  1867. if (!status)
  1868. *new_inode = inode;
  1869. ocfs2_free_dir_lookup_result(&orphan_insert);
  1870. ocfs2_inode_unlock(dir, 1);
  1871. brelse(parent_di_bh);
  1872. return status;
  1873. }
  1874. int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
  1875. struct inode *inode,
  1876. struct dentry *dentry)
  1877. {
  1878. int status = 0;
  1879. struct buffer_head *parent_di_bh = NULL;
  1880. handle_t *handle = NULL;
  1881. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1882. struct ocfs2_dinode *dir_di, *di;
  1883. struct inode *orphan_dir_inode = NULL;
  1884. struct buffer_head *orphan_dir_bh = NULL;
  1885. struct buffer_head *di_bh = NULL;
  1886. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1887. mlog_entry("(0x%p, 0x%p, %.*s')\n", dir, dentry,
  1888. dentry->d_name.len, dentry->d_name.name);
  1889. status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
  1890. if (status < 0) {
  1891. if (status != -ENOENT)
  1892. mlog_errno(status);
  1893. return status;
  1894. }
  1895. dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
  1896. if (!dir_di->i_links_count) {
  1897. /* can't make a file in a deleted directory. */
  1898. status = -ENOENT;
  1899. goto leave;
  1900. }
  1901. status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
  1902. dentry->d_name.len);
  1903. if (status)
  1904. goto leave;
  1905. /* get a spot inside the dir. */
  1906. status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
  1907. dentry->d_name.name,
  1908. dentry->d_name.len, &lookup);
  1909. if (status < 0) {
  1910. mlog_errno(status);
  1911. goto leave;
  1912. }
  1913. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1914. ORPHAN_DIR_SYSTEM_INODE,
  1915. osb->slot_num);
  1916. if (!orphan_dir_inode) {
  1917. status = -EEXIST;
  1918. mlog_errno(status);
  1919. goto leave;
  1920. }
  1921. mutex_lock(&orphan_dir_inode->i_mutex);
  1922. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  1923. if (status < 0) {
  1924. mlog_errno(status);
  1925. mutex_unlock(&orphan_dir_inode->i_mutex);
  1926. iput(orphan_dir_inode);
  1927. goto leave;
  1928. }
  1929. status = ocfs2_read_inode_block(inode, &di_bh);
  1930. if (status < 0) {
  1931. mlog_errno(status);
  1932. goto orphan_unlock;
  1933. }
  1934. handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
  1935. if (IS_ERR(handle)) {
  1936. status = PTR_ERR(handle);
  1937. handle = NULL;
  1938. mlog_errno(status);
  1939. goto orphan_unlock;
  1940. }
  1941. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  1942. di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  1943. if (status < 0) {
  1944. mlog_errno(status);
  1945. goto out_commit;
  1946. }
  1947. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  1948. orphan_dir_bh);
  1949. if (status < 0) {
  1950. mlog_errno(status);
  1951. goto out_commit;
  1952. }
  1953. di = (struct ocfs2_dinode *)di_bh->b_data;
  1954. le32_add_cpu(&di->i_flags, -OCFS2_ORPHANED_FL);
  1955. di->i_orphaned_slot = 0;
  1956. inode->i_nlink = 1;
  1957. ocfs2_set_links_count(di, inode->i_nlink);
  1958. ocfs2_journal_dirty(handle, di_bh);
  1959. status = ocfs2_add_entry(handle, dentry, inode,
  1960. OCFS2_I(inode)->ip_blkno, parent_di_bh,
  1961. &lookup);
  1962. if (status < 0) {
  1963. mlog_errno(status);
  1964. goto out_commit;
  1965. }
  1966. status = ocfs2_dentry_attach_lock(dentry, inode,
  1967. OCFS2_I(dir)->ip_blkno);
  1968. if (status) {
  1969. mlog_errno(status);
  1970. goto out_commit;
  1971. }
  1972. dentry->d_op = &ocfs2_dentry_ops;
  1973. d_instantiate(dentry, inode);
  1974. status = 0;
  1975. out_commit:
  1976. ocfs2_commit_trans(osb, handle);
  1977. orphan_unlock:
  1978. ocfs2_inode_unlock(orphan_dir_inode, 1);
  1979. mutex_unlock(&orphan_dir_inode->i_mutex);
  1980. iput(orphan_dir_inode);
  1981. leave:
  1982. ocfs2_inode_unlock(dir, 1);
  1983. brelse(di_bh);
  1984. brelse(parent_di_bh);
  1985. brelse(orphan_dir_bh);
  1986. ocfs2_free_dir_lookup_result(&lookup);
  1987. mlog_exit(status);
  1988. return status;
  1989. }
  1990. const struct inode_operations ocfs2_dir_iops = {
  1991. .create = ocfs2_create,
  1992. .lookup = ocfs2_lookup,
  1993. .link = ocfs2_link,
  1994. .unlink = ocfs2_unlink,
  1995. .rmdir = ocfs2_unlink,
  1996. .symlink = ocfs2_symlink,
  1997. .mkdir = ocfs2_mkdir,
  1998. .mknod = ocfs2_mknod,
  1999. .rename = ocfs2_rename,
  2000. .setattr = ocfs2_setattr,
  2001. .getattr = ocfs2_getattr,
  2002. .permission = ocfs2_permission,
  2003. .setxattr = generic_setxattr,
  2004. .getxattr = generic_getxattr,
  2005. .listxattr = ocfs2_listxattr,
  2006. .removexattr = generic_removexattr,
  2007. .fiemap = ocfs2_fiemap,
  2008. };