namei.c 55 KB

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