namei.c 54 KB

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