namei.c 60 KB

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