namei.c 55 KB

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