namei.c 55 KB

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