ext2subs.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * ext2subs.c version 0.20
  11. *
  12. * Some strategic functions come from linux/fs/ext2
  13. * kernel sources written by Remy Card.
  14. *
  15. */
  16. #include <u.h>
  17. #include <libc.h>
  18. #include <bio.h>
  19. #include <fcall.h>
  20. #include <thread.h>
  21. #include <9p.h>
  22. #include "dat.h"
  23. #include "fns.h"
  24. #define putext2(e) putbuf((e).buf)
  25. #define dirtyext2(e) dirtybuf((e).buf)
  26. static Intmap *uidmap, *gidmap;
  27. static int
  28. getnum(char *s, int *n)
  29. {
  30. char *r;
  31. *n = strtol(s, &r, 10);
  32. return (r != s);
  33. }
  34. static Intmap*
  35. idfile(char *f)
  36. {
  37. Biobuf *bin;
  38. Intmap *map;
  39. char *fields[3];
  40. char *s;
  41. int nf, id;
  42. map = allocmap(0);
  43. bin = Bopen(f, OREAD);
  44. if (bin == 0)
  45. return 0;
  46. while ((s = Brdline(bin, '\n')) != 0) {
  47. s[Blinelen(bin)-1] = '\0';
  48. nf = getfields(s, fields, 3, 0, ":");
  49. if (nf == 3 && getnum(fields[2], &id))
  50. insertkey(map, id, strdup(fields[0]));
  51. }
  52. Bterm(bin);
  53. return map;
  54. }
  55. void
  56. uidfile(char *f)
  57. {
  58. uidmap = idfile(f);
  59. }
  60. void
  61. gidfile(char *f)
  62. {
  63. gidmap = idfile(f);
  64. }
  65. static char*
  66. mapuid(int id)
  67. {
  68. static char s[12];
  69. char *p;
  70. if (uidmap && (p = lookupkey(uidmap, id)) != 0)
  71. return p;
  72. sprint(s, "%d", id);
  73. return s;
  74. }
  75. static char*
  76. mapgid(int id)
  77. {
  78. static char s[12];
  79. char *p;
  80. if (gidmap && (p = lookupkey(gidmap, id)) != 0)
  81. return p;
  82. sprint(s, "%d", id);
  83. return s;
  84. }
  85. int
  86. ext2fs(Xfs *xf)
  87. {
  88. SuperBlock superblock;
  89. /* get the super block */
  90. seek(xf->dev, OFFSET_SUPER_BLOCK, 0);
  91. if( sizeof(SuperBlock) !=
  92. read(xf->dev, &superblock, sizeof(SuperBlock)) ){
  93. chat("can't read super block %r...", xf->dev);
  94. errno = Eformat;
  95. return -1;
  96. }
  97. if( superblock.s_magic != EXT2_SUPER_MAGIC ){
  98. chat("Bad super block...");
  99. errno = Eformat;
  100. return -1;
  101. }
  102. if( !(superblock.s_state & EXT2_VALID_FS) ){
  103. chat("fs not checked...");
  104. errno = Enotclean;
  105. return -1;
  106. }
  107. xf->block_size = EXT2_MIN_BLOCK_SIZE << superblock.s_log_block_size;
  108. xf->desc_per_block = xf->block_size / sizeof (GroupDesc);
  109. xf->inodes_per_group = superblock.s_inodes_per_group;
  110. xf->inodes_per_block = xf->block_size / sizeof (Inode);
  111. xf->addr_per_block = xf->block_size / sizeof (uint);
  112. xf->blocks_per_group = superblock.s_blocks_per_group;
  113. if( xf->block_size == OFFSET_SUPER_BLOCK )
  114. xf->superaddr = 1, xf->superoff = 0, xf->grpaddr = 2;
  115. else if( xf->block_size == 2*OFFSET_SUPER_BLOCK ||
  116. xf->block_size == 4*OFFSET_SUPER_BLOCK )
  117. xf->superaddr = 0, xf->superoff = OFFSET_SUPER_BLOCK, xf->grpaddr = 1;
  118. else {
  119. chat(" blocks of %d bytes are not supported...", xf->block_size);
  120. errno = Eformat;
  121. return -1;
  122. }
  123. chat("good super block...");
  124. xf->ngroups = (superblock.s_blocks_count -
  125. superblock.s_first_data_block +
  126. superblock.s_blocks_per_group -1) /
  127. superblock.s_blocks_per_group;
  128. superblock.s_state &= ~EXT2_VALID_FS;
  129. superblock.s_mnt_count++;
  130. seek(xf->dev, OFFSET_SUPER_BLOCK, 0);
  131. if( !rdonly && sizeof(SuperBlock) !=
  132. write(xf->dev, &superblock, sizeof(SuperBlock)) ){
  133. chat("can't write super block...");
  134. errno = Eio;
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. Ext2
  140. getext2(Xfs *xf, char type, int n)
  141. {
  142. Iobuf *bd;
  143. Ext2 e;
  144. switch(type){
  145. case EXT2_SUPER:
  146. e.buf = getbuf(xf, xf->superaddr);
  147. if( !e.buf ) goto error;
  148. e.u.sb = (SuperBlock *)(e.buf->iobuf + xf->superoff);
  149. e.type = EXT2_SUPER;
  150. break;
  151. case EXT2_DESC:
  152. e.buf = getbuf(xf, DESC_ADDR(xf, n));
  153. if( !e.buf ) goto error;
  154. e.u.gd = DESC_OFFSET(xf, e.buf->iobuf, n);
  155. e.type = EXT2_DESC;
  156. break;
  157. case EXT2_BBLOCK:
  158. bd = getbuf(xf, DESC_ADDR(xf, n));
  159. if( !bd ) goto error;
  160. e.buf = getbuf(xf, DESC_OFFSET(xf, bd->iobuf, n)->bg_block_bitmap);
  161. if( !e.buf ){
  162. putbuf(bd);
  163. goto error;
  164. }
  165. putbuf(bd);
  166. e.u.bmp = (char *)e.buf->iobuf;
  167. e.type = EXT2_BBLOCK;
  168. break;
  169. case EXT2_BINODE:
  170. bd = getbuf(xf, DESC_ADDR(xf, n));
  171. if( !bd ) goto error;
  172. e.buf = getbuf(xf, DESC_OFFSET(xf, bd->iobuf, n)->bg_inode_bitmap);
  173. if( !e.buf ){
  174. putbuf(bd);
  175. goto error;
  176. }
  177. putbuf(bd);
  178. e.u.bmp = (char *)e.buf->iobuf;
  179. e.type = EXT2_BINODE;
  180. break;
  181. default:
  182. goto error;
  183. }
  184. error:
  185. panic("getext2");
  186. e.type = EXT2_UNKNOWN;
  187. return e;
  188. }
  189. int
  190. get_inode( Xfile *file, uint nr )
  191. {
  192. unsigned long block_group, block;
  193. Xfs *xf = file->xf;
  194. Ext2 ed, es;
  195. es = getext2(xf, EXT2_SUPER, 0);
  196. if(nr > es.u.sb->s_inodes_count ){
  197. chat("inode number %d is too big...", nr);
  198. putext2(es);
  199. errno = Eio;
  200. return -1;
  201. }
  202. putext2(es);
  203. block_group = (nr - 1) / xf->inodes_per_group;
  204. if( block_group >= xf->ngroups ){
  205. chat("block group (%d) > groups count...", block_group);
  206. errno = Eio;
  207. return -1;
  208. }
  209. ed = getext2(xf, EXT2_DESC, block_group);
  210. block = ed.u.gd->bg_inode_table + (((nr-1) % xf->inodes_per_group) /
  211. xf->inodes_per_block);
  212. putext2(ed);
  213. file->bufoffset = (nr-1) % xf->inodes_per_block;
  214. file->inbr = nr;
  215. file->bufaddr= block;
  216. return 1;
  217. }
  218. int
  219. get_file( Xfile *f, char *name)
  220. {
  221. uint offset, nr, i;
  222. Xfs *xf = f->xf;
  223. Inode *inode;
  224. int nblock;
  225. DirEntry *dir;
  226. Iobuf *buf, *ibuf;
  227. if( !S_ISDIR(getmode(f)) )
  228. return -1;
  229. ibuf = getbuf(xf, f->bufaddr);
  230. if( !ibuf )
  231. return -1;
  232. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  233. nblock = (inode->i_blocks * 512) / xf->block_size;
  234. for(i=0 ; (i < nblock) && (i < EXT2_NDIR_BLOCKS) ; i++){
  235. buf = getbuf(xf, inode->i_block[i]);
  236. if( !buf ){
  237. putbuf(ibuf);
  238. return -1;
  239. }
  240. for(offset=0 ; offset < xf->block_size ; ){
  241. dir = (DirEntry *)(buf->iobuf + offset);
  242. if( dir->name_len==strlen(name) &&
  243. !strncmp(name, dir->name, dir->name_len) ){
  244. nr = dir->inode;
  245. putbuf(buf);
  246. putbuf(ibuf);
  247. return nr;
  248. }
  249. offset += dir->rec_len;
  250. }
  251. putbuf(buf);
  252. }
  253. putbuf(ibuf);
  254. errno = Enonexist;
  255. return -1;
  256. }
  257. char *
  258. getname(Xfile *f, char *str)
  259. {
  260. Xfile ft;
  261. int offset, i, len;
  262. Xfs *xf = f->xf;
  263. Inode *inode;
  264. int nblock;
  265. DirEntry *dir;
  266. Iobuf *buf, *ibuf;
  267. ft = *f;
  268. if( get_inode(&ft, f->pinbr) < 0 )
  269. return 0;
  270. if( !S_ISDIR(getmode(&ft)) )
  271. return 0;
  272. ibuf = getbuf(xf, ft.bufaddr);
  273. if( !ibuf )
  274. return 0;
  275. inode = ((Inode *)ibuf->iobuf) + ft.bufoffset;
  276. nblock = (inode->i_blocks * 512) / xf->block_size;
  277. for(i=0 ; (i < nblock) && (i < EXT2_NDIR_BLOCKS) ; i++){
  278. buf = getbuf(xf, inode->i_block[i]);
  279. if( !buf ){
  280. putbuf(ibuf);
  281. return 0;
  282. }
  283. for(offset=0 ; offset < xf->block_size ; ){
  284. dir = (DirEntry *)(buf->iobuf + offset);
  285. if( f->inbr == dir->inode ){
  286. len = (dir->name_len < EXT2_NAME_LEN) ? dir->name_len : EXT2_NAME_LEN;
  287. if (str == 0)
  288. str = malloc(len+1);
  289. strncpy(str, dir->name, len);
  290. str[len] = 0;
  291. putbuf(buf);
  292. putbuf(ibuf);
  293. return str;
  294. }
  295. offset += dir->rec_len;
  296. }
  297. putbuf(buf);
  298. }
  299. putbuf(ibuf);
  300. errno = Enonexist;
  301. return 0;
  302. }
  303. void
  304. dostat(Qid qid, Xfile *f, Dir *dir )
  305. {
  306. Inode *inode;
  307. Iobuf *ibuf;
  308. char *name;
  309. memset(dir, 0, sizeof(Dir));
  310. if( f->inbr == EXT2_ROOT_INODE ){
  311. dir->name = estrdup9p("/");
  312. dir->qid = (Qid){0,0,QTDIR};
  313. dir->mode = DMDIR | 0777;
  314. }else{
  315. ibuf = getbuf(f->xf, f->bufaddr);
  316. if( !ibuf )
  317. return;
  318. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  319. dir->length = inode->i_size;
  320. dir->atime = inode->i_atime;
  321. dir->mtime = inode->i_mtime;
  322. putbuf(ibuf);
  323. name = getname(f, 0);
  324. dir->name = name;
  325. dir->uid = estrdup9p(mapuid(inode->i_uid));
  326. dir->gid = estrdup9p(mapgid(inode->i_gid));
  327. dir->qid = qid;
  328. dir->mode = getmode(f);
  329. if( qid.type & QTDIR )
  330. dir->mode |= DMDIR;
  331. }
  332. }
  333. int
  334. dowstat(Xfile *f, Dir *stat)
  335. {
  336. Xfs *xf = f->xf;
  337. Inode *inode;
  338. Xfile fdir;
  339. Iobuf *ibuf;
  340. char name[EXT2_NAME_LEN+1];
  341. /* change name */
  342. getname(f, name);
  343. if( stat->name && stat->name[0] != 0 && strcmp(name, stat->name) ){
  344. /* get dir */
  345. fdir = *f;
  346. if( get_inode(&fdir, f->pinbr) < 0 ){
  347. chat("can't get inode %d...", f->pinbr);
  348. return -1;
  349. }
  350. ibuf = getbuf(xf, fdir.bufaddr);
  351. if( !ibuf )
  352. return -1;
  353. inode = ((Inode *)ibuf->iobuf) +fdir.bufoffset;
  354. /* Clean old dir entry */
  355. if( delete_entry(xf, inode, f->inbr) < 0 ){
  356. chat("delete entry failed...");
  357. putbuf(ibuf);
  358. return -1;
  359. }
  360. putbuf(ibuf);
  361. /* add the new entry */
  362. if( add_entry(&fdir, stat->name, f->inbr) < 0 ){
  363. chat("add entry failed...");
  364. return -1;
  365. }
  366. }
  367. ibuf = getbuf(xf, f->bufaddr);
  368. if( !ibuf )
  369. return -1;
  370. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  371. if (stat->mode != ~0)
  372. if( (getmode(f) & 0777) != (stat->mode & 0777) ){
  373. inode->i_mode = (getmode(f) & ~0777) | (stat->mode & 0777);
  374. dirtybuf(ibuf);
  375. }
  376. if (stat->mtime != ~0)
  377. if( inode->i_mtime != stat->mtime ){
  378. inode->i_mtime = stat->mtime;
  379. dirtybuf(ibuf);
  380. }
  381. putbuf(ibuf);
  382. return 1;
  383. }
  384. int32_t
  385. readfile(Xfile *f, void *vbuf, int64_t offset, int32_t count)
  386. {
  387. Xfs *xf = f->xf;
  388. Inode *inode;
  389. Iobuf *buffer, *ibuf;
  390. int32_t rcount;
  391. int len, o, cur_block, baddr;
  392. uint8_t *buf;
  393. buf = vbuf;
  394. ibuf = getbuf(xf, f->bufaddr);
  395. if( !ibuf )
  396. return -1;
  397. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  398. if( offset >= inode->i_size ){
  399. putbuf(ibuf);
  400. return 0;
  401. }
  402. if( offset + count > inode->i_size )
  403. count = inode->i_size - offset;
  404. /* fast link */
  405. if( S_ISLNK(getmode(f)) && (inode->i_size <= EXT2_N_BLOCKS<<2) ){
  406. memcpy(&buf[0], ((char *)inode->i_block)+offset, count);
  407. putbuf(ibuf);
  408. return count;
  409. }
  410. chat("read block [ ");
  411. cur_block = offset / xf->block_size;
  412. o = offset % xf->block_size;
  413. rcount = 0;
  414. while( count > 0 ){
  415. baddr = bmap(f, cur_block++);
  416. if( !baddr ){
  417. putbuf(ibuf);
  418. return -1;
  419. }
  420. buffer = getbuf(xf, baddr);
  421. if( !buffer ){
  422. putbuf(ibuf);
  423. return -1;
  424. }
  425. chat("%d ", baddr);
  426. len = xf->block_size - o;
  427. if( len > count )
  428. len = count;
  429. memcpy(&buf[rcount], &buffer->iobuf[o], len);
  430. rcount += len;
  431. count -= len;
  432. o = 0;
  433. putbuf(buffer);
  434. }
  435. chat("] ...");
  436. inode->i_atime = time(0);
  437. dirtybuf(ibuf);
  438. putbuf(ibuf);
  439. return rcount;
  440. }
  441. int32_t
  442. readdir(Xfile *f, void *vbuf, int64_t offset, int32_t count)
  443. {
  444. int off, i, len;
  445. int32_t rcount;
  446. Xfs *xf = f->xf;
  447. Inode *inode, *tinode;
  448. int nblock;
  449. DirEntry *edir;
  450. Iobuf *buffer, *ibuf, *tbuf;
  451. Dir pdir;
  452. Xfile ft;
  453. uint8_t *buf;
  454. char name[EXT2_NAME_LEN+1];
  455. unsigned int dirlen;
  456. int index;
  457. buf = vbuf;
  458. if (offset == 0)
  459. f->dirindex = 0;
  460. if( !S_ISDIR(getmode(f)) )
  461. return -1;
  462. ibuf = getbuf(xf, f->bufaddr);
  463. if( !ibuf )
  464. return -1;
  465. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  466. nblock = (inode->i_blocks * 512) / xf->block_size;
  467. ft = *f;
  468. chat("read block [ ");
  469. index = 0;
  470. for(i=0, rcount=0 ; (i < nblock) && (i < EXT2_NDIR_BLOCKS) ; i++){
  471. buffer = getbuf(xf, inode->i_block[i]);
  472. if( !buffer ){
  473. putbuf(ibuf);
  474. return -1;
  475. }
  476. chat("%d, ", buffer->addr);
  477. for(off=0 ; off < xf->block_size ; ){
  478. edir = (DirEntry *)(buffer->iobuf + off);
  479. off += edir->rec_len;
  480. if( (edir->name[0] == '.' ) && (edir->name_len == 1))
  481. continue;
  482. if(edir->name[0] == '.' && edir->name[1] == '.' &&
  483. edir->name_len == 2)
  484. continue;
  485. if( edir->inode == 0 ) /* for lost+found dir ... */
  486. continue;
  487. if( index++ < f->dirindex )
  488. continue;
  489. if( get_inode(&ft, edir->inode) < 0 ){
  490. chat("can't find ino no %d ] ...", edir->inode);
  491. error: putbuf(buffer);
  492. putbuf(ibuf);
  493. return -1;
  494. }
  495. tbuf = getbuf(xf, ft.bufaddr);
  496. if( !tbuf )
  497. goto error;
  498. tinode = ((Inode *)tbuf->iobuf) + ft.bufoffset;
  499. memset(&pdir, 0, sizeof(Dir));
  500. /* fill plan9 dir struct */
  501. pdir.name = name;
  502. len = (edir->name_len < EXT2_NAME_LEN) ? edir->name_len : EXT2_NAME_LEN;
  503. strncpy(pdir.name, edir->name, len);
  504. pdir.name[len] = 0;
  505. // chat("name %s len %d\n", pdir.name, edir->name_len);
  506. pdir.uid = mapuid(tinode->i_uid);
  507. pdir.gid = mapgid(tinode->i_gid);
  508. pdir.qid.path = edir->inode;
  509. pdir.mode = tinode->i_mode;
  510. if( edir->inode == EXT2_ROOT_INODE )
  511. pdir.qid.path = f->xf->rootqid.path;
  512. else if( S_ISDIR( tinode->i_mode) )
  513. pdir.qid.type |= QTDIR;
  514. if( pdir.qid.type & QTDIR )
  515. pdir.mode |= DMDIR;
  516. pdir.length = tinode->i_size;
  517. pdir.atime = tinode->i_atime;
  518. pdir.mtime = tinode->i_mtime;
  519. putbuf(tbuf);
  520. dirlen = convD2M(&pdir, &buf[rcount], count-rcount);
  521. if ( dirlen <= BIT16SZ ) {
  522. chat("] ...");
  523. putbuf(buffer);
  524. putbuf(ibuf);
  525. return rcount;
  526. }
  527. rcount += dirlen;
  528. f->dirindex++;
  529. }
  530. putbuf(buffer);
  531. }
  532. chat("] ...");
  533. putbuf(ibuf);
  534. return rcount;
  535. }
  536. int
  537. bmap( Xfile *f, int block )
  538. {
  539. Xfs *xf = f->xf;
  540. Inode *inode;
  541. Iobuf *buf, *ibuf;
  542. int addr;
  543. int addr_per_block = xf->addr_per_block;
  544. int addr_per_block_bits = ffz(~addr_per_block);
  545. if(block < 0) {
  546. chat("bmap() block < 0 ...");
  547. return 0;
  548. }
  549. if(block >= EXT2_NDIR_BLOCKS + addr_per_block +
  550. (1 << (addr_per_block_bits * 2)) +
  551. ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
  552. chat("bmap() block > big...");
  553. return 0;
  554. }
  555. ibuf = getbuf(xf, f->bufaddr);
  556. if( !ibuf )
  557. return 0;
  558. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  559. /* direct blocks */
  560. if(block < EXT2_NDIR_BLOCKS){
  561. putbuf(ibuf);
  562. return inode->i_block[block];
  563. }
  564. block -= EXT2_NDIR_BLOCKS;
  565. /* indirect blocks*/
  566. if(block < addr_per_block) {
  567. addr = inode->i_block[EXT2_IND_BLOCK];
  568. if (!addr) goto error;
  569. buf = getbuf(xf, addr);
  570. if( !buf ) goto error;
  571. addr = *(((uint *)buf->iobuf) + block);
  572. putbuf(buf);
  573. putbuf(ibuf);
  574. return addr;
  575. }
  576. block -= addr_per_block;
  577. /* double indirect blocks */
  578. if(block < (1 << (addr_per_block_bits * 2))) {
  579. addr = inode->i_block[EXT2_DIND_BLOCK];
  580. if (!addr) goto error;
  581. buf = getbuf(xf, addr);
  582. if( !buf ) goto error;
  583. addr = *(((uint *)buf->iobuf) + (block >> addr_per_block_bits));
  584. putbuf(buf);
  585. buf = getbuf(xf, addr);
  586. if( !buf ) goto error;
  587. addr = *(((uint *)buf->iobuf) + (block & (addr_per_block - 1)));
  588. putbuf(buf);
  589. putbuf(ibuf);
  590. return addr;
  591. }
  592. block -= (1 << (addr_per_block_bits * 2));
  593. /* triple indirect blocks */
  594. addr = inode->i_block[EXT2_TIND_BLOCK];
  595. if(!addr) goto error;
  596. buf = getbuf(xf, addr);
  597. if( !buf ) goto error;
  598. addr = *(((uint *)buf->iobuf) + (block >> (addr_per_block_bits * 2)));
  599. putbuf(buf);
  600. if(!addr) goto error;
  601. buf = getbuf(xf, addr);
  602. if( !buf ) goto error;
  603. addr = *(((uint *)buf->iobuf) +
  604. ((block >> addr_per_block_bits) & (addr_per_block - 1)));
  605. putbuf(buf);
  606. if(!addr) goto error;
  607. buf = getbuf(xf, addr);
  608. if( !buf ) goto error;
  609. addr = *(((uint *)buf->iobuf) + (block & (addr_per_block - 1)));
  610. putbuf(buf);
  611. putbuf(ibuf);
  612. return addr;
  613. error:
  614. putbuf(ibuf);
  615. return 0;
  616. }
  617. int32_t
  618. writefile(Xfile *f, void *vbuf, int64_t offset, int32_t count)
  619. {
  620. Xfs *xf = f->xf;
  621. Inode *inode;
  622. Iobuf *buffer, *ibuf;
  623. int32_t w;
  624. int len, o, cur_block, baddr;
  625. char *buf;
  626. buf = vbuf;
  627. ibuf = getbuf(xf, f->bufaddr);
  628. if( !ibuf )
  629. return -1;
  630. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  631. chat("write block [ ");
  632. cur_block = offset / xf->block_size;
  633. o = offset % xf->block_size;
  634. w = 0;
  635. while( count > 0 ){
  636. baddr = getblk(f, cur_block++);
  637. if( baddr <= 0 )
  638. goto end;
  639. buffer = getbuf(xf, baddr);
  640. if( !buffer )
  641. goto end;
  642. chat("%d ", baddr);
  643. len = xf->block_size - o;
  644. if( len > count )
  645. len = count;
  646. memcpy(&buffer->iobuf[o], &buf[w], len);
  647. dirtybuf(buffer);
  648. w += len;
  649. count -= len;
  650. o = 0;
  651. putbuf(buffer);
  652. }
  653. end:
  654. if( inode->i_size < offset + w )
  655. inode->i_size = offset + w;
  656. inode->i_atime = inode->i_mtime = time(0);
  657. dirtybuf(ibuf);
  658. putbuf(ibuf);
  659. chat("]...");
  660. if( errno )
  661. return -1;
  662. return w;
  663. }
  664. int
  665. new_block( Xfile *f, int goal )
  666. {
  667. Xfs *xf= f->xf;
  668. int group, block, baddr, k, redo;
  669. uint32_t lmap;
  670. char *p, *r;
  671. Iobuf *buf;
  672. Ext2 ed, es, eb;
  673. es = getext2(xf, EXT2_SUPER, 0);
  674. redo = 0;
  675. repeat:
  676. if( goal < es.u.sb->s_first_data_block || goal >= es.u.sb->s_blocks_count )
  677. goal = es.u.sb->s_first_data_block;
  678. group = (goal - es.u.sb->s_first_data_block) / xf->blocks_per_group;
  679. ed = getext2(xf, EXT2_DESC, group);
  680. eb = getext2(xf, EXT2_BBLOCK, group);
  681. /*
  682. * First, test if goal block is free
  683. */
  684. if( ed.u.gd->bg_free_blocks_count > 0 ){
  685. block = (goal - es.u.sb->s_first_data_block) % xf->blocks_per_group;
  686. if( !test_bit(block, eb.u.bmp) )
  687. goto got_block;
  688. if( block ){
  689. /*
  690. * goal wasn't free ; search foward for a free
  691. * block within the next 32 blocks
  692. */
  693. lmap = (((uint32_t *)eb.u.bmp)[block>>5]) >>
  694. ((block & 31) + 1);
  695. if( block < xf->blocks_per_group - 32 )
  696. lmap |= (((uint32_t *)eb.u.bmp)[(block>>5)+1]) <<
  697. ( 31-(block & 31) );
  698. else
  699. lmap |= 0xffffffff << ( 31-(block & 31) );
  700. if( lmap != 0xffffffffl ){
  701. k = ffz(lmap) + 1;
  702. if( (block + k) < xf->blocks_per_group ){
  703. block += k;
  704. goto got_block;
  705. }
  706. }
  707. }
  708. /*
  709. * Search in the remaider of the group
  710. */
  711. p = eb.u.bmp + (block>>3);
  712. r = memscan(p, 0, (xf->blocks_per_group - block + 7) >>3);
  713. k = ( r - eb.u.bmp )<<3;
  714. if( k < xf->blocks_per_group ){
  715. block = k;
  716. goto search_back;
  717. }
  718. k = find_next_zero_bit((unsigned long *)eb.u.bmp,
  719. xf->blocks_per_group>>3, block);
  720. if( k < xf->blocks_per_group ){
  721. block = k;
  722. goto got_block;
  723. }
  724. }
  725. /*
  726. * Search the rest of groups
  727. */
  728. putext2(ed); putext2(eb);
  729. for(k=0 ; k < xf->ngroups ; k++){
  730. group++;
  731. if( group >= xf->ngroups )
  732. group = 0;
  733. ed = getext2(xf, EXT2_DESC, group);
  734. if( ed.u.gd->bg_free_blocks_count > 0 )
  735. break;
  736. putext2(ed);
  737. }
  738. if( redo && group == xf->ngroups-1 ){
  739. putext2(ed);
  740. goto full;
  741. }
  742. if( k >=xf->ngroups ){
  743. /*
  744. * All groups are full or
  745. * we have retry (because the last block) and all other
  746. * groups are also full.
  747. */
  748. full:
  749. chat("no free blocks ...");
  750. putext2(es);
  751. errno = Enospace;
  752. return 0;
  753. }
  754. eb = getext2(xf, EXT2_BBLOCK, group);
  755. r = memscan(eb.u.bmp, 0, xf->blocks_per_group>>3);
  756. block = (r - eb.u.bmp) <<3;
  757. if( block < xf->blocks_per_group )
  758. goto search_back;
  759. else
  760. block = find_first_zero_bit((uint32_t *)eb.u.bmp,
  761. xf->blocks_per_group>>3);
  762. if( block >= xf->blocks_per_group ){
  763. chat("Free block count courupted for block group %d...", group);
  764. putext2(ed); putext2(eb); putext2(es);
  765. errno = Ecorrupt;
  766. return 0;
  767. }
  768. search_back:
  769. /*
  770. * A free byte was found in the block. Now search backwards up
  771. * to 7 bits to find the start of this group of free block.
  772. */
  773. for(k=0 ; k < 7 && block > 0 &&
  774. !test_bit(block-1, eb.u.bmp) ; k++, block--);
  775. got_block:
  776. baddr = block + (group * xf->blocks_per_group) +
  777. es.u.sb->s_first_data_block;
  778. if( baddr == ed.u.gd->bg_block_bitmap ||
  779. baddr == ed.u.gd->bg_inode_bitmap ){
  780. chat("Allocating block in system zone...");
  781. putext2(ed); putext2(eb); putext2(es);
  782. errno = Eintern;
  783. return 0;
  784. }
  785. if( set_bit(block, eb.u.bmp) ){
  786. chat("bit already set (%d)...", block);
  787. putext2(ed); putext2(eb); putext2(es);
  788. errno = Ecorrupt;
  789. return 0;
  790. }
  791. dirtyext2(eb);
  792. if( baddr >= es.u.sb->s_blocks_count ){
  793. chat("block >= blocks count...");
  794. errno = Eintern;
  795. error:
  796. clear_bit(block, eb.u.bmp);
  797. putext2(eb); putext2(ed); putext2(es);
  798. return 0;
  799. }
  800. buf = getbuf(xf, baddr);
  801. if( !buf ){
  802. if( !redo ){
  803. /*
  804. * It's perhaps the last block of the disk and
  805. * it can't be acceded because the last sector.
  806. * Therefore, we try one more time with goal at 0
  807. * to force scanning all groups.
  808. */
  809. clear_bit(block, eb.u.bmp);
  810. putext2(eb); putext2(ed);
  811. goal = 0; errno = 0; redo++;
  812. goto repeat;
  813. }
  814. goto error;
  815. }
  816. memset(&buf->iobuf[0], 0, xf->block_size);
  817. dirtybuf(buf);
  818. putbuf(buf);
  819. es.u.sb->s_free_blocks_count--;
  820. dirtyext2(es);
  821. ed.u.gd->bg_free_blocks_count--;
  822. dirtyext2(ed);
  823. putext2(eb);
  824. putext2(ed);
  825. putext2(es);
  826. chat("new ");
  827. return baddr;
  828. }
  829. int
  830. getblk(Xfile *f, int block)
  831. {
  832. Xfs *xf = f->xf;
  833. int baddr;
  834. int addr_per_block = xf->addr_per_block;
  835. if (block < 0) {
  836. chat("getblk() block < 0 ...");
  837. return 0;
  838. }
  839. if(block > EXT2_NDIR_BLOCKS + addr_per_block +
  840. addr_per_block * addr_per_block +
  841. addr_per_block * addr_per_block * addr_per_block ){
  842. chat("getblk() block > big...");
  843. errno = Eintern;
  844. return 0;
  845. }
  846. if( block < EXT2_NDIR_BLOCKS )
  847. return inode_getblk(f, block);
  848. block -= EXT2_NDIR_BLOCKS;
  849. if( block < addr_per_block ){
  850. baddr = inode_getblk(f, EXT2_IND_BLOCK);
  851. baddr = block_getblk(f, baddr, block);
  852. return baddr;
  853. }
  854. block -= addr_per_block;
  855. if( block < addr_per_block * addr_per_block ){
  856. baddr = inode_getblk(f, EXT2_DIND_BLOCK);
  857. baddr = block_getblk(f, baddr, block / addr_per_block);
  858. baddr = block_getblk(f, baddr, block & ( addr_per_block-1));
  859. return baddr;
  860. }
  861. block -= addr_per_block * addr_per_block;
  862. baddr = inode_getblk(f, EXT2_TIND_BLOCK);
  863. baddr = block_getblk(f, baddr, block / (addr_per_block * addr_per_block));
  864. baddr = block_getblk(f, baddr, (block / addr_per_block) & ( addr_per_block-1));
  865. return block_getblk(f, baddr, block & ( addr_per_block-1));
  866. }
  867. int
  868. block_getblk(Xfile *f, int rb, int nr)
  869. {
  870. Xfs *xf = f->xf;
  871. Inode *inode;
  872. int tmp, goal = 0;
  873. int blocks = xf->block_size / 512;
  874. Iobuf *buf, *ibuf;
  875. uint *p;
  876. Ext2 es;
  877. if( !rb )
  878. return 0;
  879. buf = getbuf(xf, rb);
  880. if( !buf )
  881. return 0;
  882. p = (uint *)(buf->iobuf) + nr;
  883. if( *p ){
  884. tmp = *p;
  885. putbuf(buf);
  886. return tmp;
  887. }
  888. for(tmp=nr - 1 ; tmp >= 0 ; tmp--){
  889. if( ((uint *)(buf->iobuf))[tmp] ){
  890. goal = ((uint *)(buf->iobuf))[tmp];
  891. break;
  892. }
  893. }
  894. if( !goal ){
  895. es = getext2(xf, EXT2_SUPER, 0);
  896. goal = (((f->inbr -1) / xf->inodes_per_group) *
  897. xf->blocks_per_group) +
  898. es.u.sb->s_first_data_block;
  899. putext2(es);
  900. }
  901. tmp = new_block(f, goal);
  902. if( !tmp ){
  903. putbuf(buf);
  904. return 0;
  905. }
  906. *p = tmp;
  907. dirtybuf(buf);
  908. putbuf(buf);
  909. ibuf = getbuf(xf, f->bufaddr);
  910. if( !ibuf )
  911. return -1;
  912. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  913. inode->i_blocks += blocks;
  914. dirtybuf(ibuf);
  915. putbuf(ibuf);
  916. return tmp;
  917. }
  918. int
  919. inode_getblk(Xfile *f, int block)
  920. {
  921. Xfs *xf = f->xf;
  922. Inode *inode;
  923. Iobuf *ibuf;
  924. int tmp, goal = 0;
  925. int blocks = xf->block_size / 512;
  926. Ext2 es;
  927. ibuf = getbuf(xf, f->bufaddr);
  928. if( !ibuf )
  929. return -1;
  930. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  931. if( inode->i_block[block] ){
  932. putbuf(ibuf);
  933. return inode->i_block[block];
  934. }
  935. for(tmp=block - 1 ; tmp >= 0 ; tmp--){
  936. if( inode->i_block[tmp] ){
  937. goal = inode->i_block[tmp];
  938. break;
  939. }
  940. }
  941. if( !goal ){
  942. es = getext2(xf, EXT2_SUPER, 0);
  943. goal = (((f->inbr -1) / xf->inodes_per_group) *
  944. xf->blocks_per_group) +
  945. es.u.sb->s_first_data_block;
  946. putext2(es);
  947. }
  948. tmp = new_block(f, goal);
  949. if( !tmp ){
  950. putbuf(ibuf);
  951. return 0;
  952. }
  953. inode->i_block[block] = tmp;
  954. inode->i_blocks += blocks;
  955. dirtybuf(ibuf);
  956. putbuf(ibuf);
  957. return tmp;
  958. }
  959. int
  960. new_inode(Xfile *f, int mode)
  961. {
  962. Xfs *xf = f->xf;
  963. Inode *inode, *finode;
  964. Iobuf *buf, *ibuf;
  965. int ave,group, i, j;
  966. Ext2 ed, es, eb;
  967. group = -1;
  968. es = getext2(xf, EXT2_SUPER, 0);
  969. if( S_ISDIR(mode) ){ /* create directory inode */
  970. ave = es.u.sb->s_free_inodes_count / xf->ngroups;
  971. for(i=0 ; i < xf->ngroups ; i++){
  972. ed = getext2(xf, EXT2_DESC, i);
  973. if( ed.u.gd->bg_free_inodes_count &&
  974. ed.u.gd->bg_free_inodes_count >= ave ){
  975. if( group<0 )
  976. group = i;
  977. }
  978. putext2(ed);
  979. }
  980. }else{ /* create file inode */
  981. /* Try to put inode in its parent directory */
  982. i = (f->inbr -1) / xf->inodes_per_group;
  983. ed = getext2(xf, EXT2_DESC, i);
  984. if( ed.u.gd->bg_free_inodes_count ){
  985. group = i;
  986. putext2(ed);
  987. }else{
  988. /*
  989. * Use a quadratic hash to find a group whith
  990. * a free inode
  991. */
  992. putext2(ed);
  993. for( j=1 ; j < xf->ngroups ; j <<= 1){
  994. i += j;
  995. if( i >= xf->ngroups )
  996. i -= xf->ngroups;
  997. ed = getext2(xf, EXT2_DESC, i);
  998. if( ed.u.gd->bg_free_inodes_count ){
  999. group = i;
  1000. putext2(ed);
  1001. break;
  1002. }
  1003. putext2(ed);
  1004. }
  1005. }
  1006. if( group < 0 ){
  1007. /* try a linear search */
  1008. i = ((f->inbr -1) / xf->inodes_per_group) + 1;
  1009. for(j=2 ; j < xf->ngroups ; j++){
  1010. if( ++i >= xf->ngroups )
  1011. i = 0;
  1012. ed = getext2(xf, EXT2_DESC, i);
  1013. if( ed.u.gd->bg_free_inodes_count ){
  1014. group = i;
  1015. putext2(ed);
  1016. break;
  1017. }
  1018. putext2(ed);
  1019. }
  1020. }
  1021. }
  1022. if( group < 0 ){
  1023. chat("group < 0...");
  1024. putext2(es);
  1025. return 0;
  1026. }
  1027. ed = getext2(xf, EXT2_DESC, group);
  1028. eb = getext2(xf, EXT2_BINODE, group);
  1029. if( (j = find_first_zero_bit(eb.u.bmp,
  1030. xf->inodes_per_group>>3)) < xf->inodes_per_group){
  1031. if( set_bit(j, eb.u.bmp) ){
  1032. chat("inode %d of group %d is already allocated...", j, group);
  1033. putext2(ed); putext2(eb); putext2(es);
  1034. errno = Ecorrupt;
  1035. return 0;
  1036. }
  1037. dirtyext2(eb);
  1038. }else if( ed.u.gd->bg_free_inodes_count != 0 ){
  1039. chat("free inodes count corrupted for group %d...", group);
  1040. putext2(ed); putext2(eb); putext2(es);
  1041. errno = Ecorrupt;
  1042. return 0;
  1043. }
  1044. i = j;
  1045. j += group * xf->inodes_per_group + 1;
  1046. if( j < EXT2_FIRST_INO || j >= es.u.sb->s_inodes_count ){
  1047. chat("reserved inode or inode > inodes count...");
  1048. errno = Ecorrupt;
  1049. error:
  1050. clear_bit(i, eb.u.bmp);
  1051. putext2(eb); putext2(ed); putext2(es);
  1052. return 0;
  1053. }
  1054. buf = getbuf(xf, ed.u.gd->bg_inode_table +
  1055. (((j-1) % xf->inodes_per_group) /
  1056. xf->inodes_per_block));
  1057. if( !buf )
  1058. goto error;
  1059. inode = ((struct Inode *) buf->iobuf) +
  1060. ((j-1) % xf->inodes_per_block);
  1061. memset(inode, 0, sizeof(Inode));
  1062. inode->i_mode = mode;
  1063. inode->i_links_count = 1;
  1064. inode->i_uid = DEFAULT_UID;
  1065. inode->i_gid = DEFAULT_GID;
  1066. inode->i_mtime = inode->i_atime = inode->i_ctime = time(0);
  1067. dirtybuf(buf);
  1068. ibuf = getbuf(xf, f->bufaddr);
  1069. if( !ibuf ){
  1070. putbuf(buf);
  1071. goto error;
  1072. }
  1073. finode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  1074. inode->i_flags = finode->i_flags;
  1075. inode->i_uid = finode->i_uid;
  1076. inode->i_gid = finode->i_gid;
  1077. dirtybuf(ibuf);
  1078. putbuf(ibuf);
  1079. putbuf(buf);
  1080. ed.u.gd->bg_free_inodes_count--;
  1081. if( S_ISDIR(mode) )
  1082. ed.u.gd->bg_used_dirs_count++;
  1083. dirtyext2(ed);
  1084. es.u.sb->s_free_inodes_count--;
  1085. dirtyext2(es);
  1086. putext2(eb);
  1087. putext2(ed);
  1088. putext2(es);
  1089. return j;
  1090. }
  1091. int
  1092. create_file(Xfile *fdir, char *name, int mode)
  1093. {
  1094. int inr;
  1095. inr = new_inode(fdir, mode);
  1096. if( !inr ){
  1097. chat("create one new inode failed...");
  1098. return -1;
  1099. }
  1100. if( add_entry(fdir, name, inr) < 0 ){
  1101. chat("add entry failed...");
  1102. free_inode(fdir->xf, inr);
  1103. return -1;
  1104. }
  1105. return inr;
  1106. }
  1107. void
  1108. free_inode( Xfs *xf, int inr)
  1109. {
  1110. Inode *inode;
  1111. uint32_t b, bg;
  1112. Iobuf *buf;
  1113. Ext2 ed, es, eb;
  1114. bg = (inr -1) / xf->inodes_per_group;
  1115. b = (inr -1) % xf->inodes_per_group;
  1116. ed = getext2(xf, EXT2_DESC, bg);
  1117. buf = getbuf(xf, ed.u.gd->bg_inode_table +
  1118. (b / xf->inodes_per_block));
  1119. if( !buf ){
  1120. putext2(ed);
  1121. return;
  1122. }
  1123. inode = ((struct Inode *) buf->iobuf) +
  1124. ((inr-1) % xf->inodes_per_block);
  1125. if( S_ISDIR(inode->i_mode) )
  1126. ed.u.gd->bg_used_dirs_count--;
  1127. memset(inode, 0, sizeof(Inode));
  1128. inode->i_dtime = time(0);
  1129. dirtybuf(buf);
  1130. putbuf(buf);
  1131. ed.u.gd->bg_free_inodes_count++;
  1132. dirtyext2(ed);
  1133. putext2(ed);
  1134. eb = getext2(xf, EXT2_BINODE, bg);
  1135. clear_bit(b, eb.u.bmp);
  1136. dirtyext2(eb);
  1137. putext2(eb);
  1138. es = getext2(xf, EXT2_SUPER, 0);
  1139. es.u.sb->s_free_inodes_count++;
  1140. dirtyext2(es); putext2(es);
  1141. }
  1142. int
  1143. create_dir(Xfile *fdir, char *name, int mode)
  1144. {
  1145. Xfs *xf = fdir->xf;
  1146. DirEntry *de;
  1147. Inode *inode;
  1148. Iobuf *buf, *ibuf;
  1149. Xfile tf;
  1150. int inr, baddr;
  1151. inr = new_inode(fdir, mode);
  1152. if( inr == 0 ){
  1153. chat("create one new inode failed...");
  1154. return -1;
  1155. }
  1156. if( add_entry(fdir, name, inr) < 0 ){
  1157. chat("add entry failed...");
  1158. free_inode(fdir->xf, inr);
  1159. return -1;
  1160. }
  1161. /* create the empty dir */
  1162. tf = *fdir;
  1163. if( get_inode(&tf, inr) < 0 ){
  1164. chat("can't get inode %d...", inr);
  1165. free_inode(fdir->xf, inr);
  1166. return -1;
  1167. }
  1168. ibuf = getbuf(xf, tf.bufaddr);
  1169. if( !ibuf ){
  1170. free_inode(fdir->xf, inr);
  1171. return -1;
  1172. }
  1173. inode = ((Inode *)ibuf->iobuf) + tf.bufoffset;
  1174. baddr = inode_getblk(&tf, 0);
  1175. if( !baddr ){
  1176. putbuf(ibuf);
  1177. ibuf = getbuf(xf, fdir->bufaddr);
  1178. if( !ibuf ){
  1179. free_inode(fdir->xf, inr);
  1180. return -1;
  1181. }
  1182. inode = ((Inode *)ibuf->iobuf) + fdir->bufoffset;
  1183. delete_entry(fdir->xf, inode, inr);
  1184. putbuf(ibuf);
  1185. free_inode(fdir->xf, inr);
  1186. return -1;
  1187. }
  1188. inode->i_size = xf->block_size;
  1189. buf = getbuf(xf, baddr);
  1190. de = (DirEntry *)buf->iobuf;
  1191. de->inode = inr;
  1192. de->name_len = 1;
  1193. de->rec_len = DIR_REC_LEN(de->name_len);
  1194. strcpy(de->name, ".");
  1195. de = (DirEntry *)( (char *)de + de->rec_len);
  1196. de->inode = fdir->inbr;
  1197. de->name_len = 2;
  1198. de->rec_len = xf->block_size - DIR_REC_LEN(1);
  1199. strcpy(de->name, "..");
  1200. dirtybuf(buf);
  1201. putbuf(buf);
  1202. inode->i_links_count = 2;
  1203. dirtybuf(ibuf);
  1204. putbuf(ibuf);
  1205. ibuf = getbuf(xf, fdir->bufaddr);
  1206. if( !ibuf )
  1207. return -1;
  1208. inode = ((Inode *)ibuf->iobuf) + fdir->bufoffset;
  1209. inode->i_links_count++;
  1210. dirtybuf(ibuf);
  1211. putbuf(ibuf);
  1212. return inr;
  1213. }
  1214. int
  1215. add_entry(Xfile *f, char *name, int inr)
  1216. {
  1217. Xfs *xf = f->xf;
  1218. DirEntry *de, *de1;
  1219. int offset, baddr;
  1220. int rec_len, cur_block;
  1221. int namelen = strlen(name);
  1222. Inode *inode;
  1223. Iobuf *buf, *ibuf;
  1224. ibuf = getbuf(xf, f->bufaddr);
  1225. if( !ibuf )
  1226. return -1;
  1227. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  1228. if( inode->i_size == 0 ){
  1229. chat("add_entry() no entry !!!...");
  1230. putbuf(ibuf);
  1231. return -1;
  1232. }
  1233. cur_block = offset = 0;
  1234. rec_len = DIR_REC_LEN(namelen);
  1235. buf = getbuf(xf, inode->i_block[cur_block++]);
  1236. if( !buf ){
  1237. putbuf(ibuf);
  1238. return -1;
  1239. }
  1240. de = (DirEntry *)buf->iobuf;
  1241. for(;;){
  1242. if( ((char *)de) >= (xf->block_size + buf->iobuf) ){
  1243. putbuf(buf);
  1244. if( cur_block >= EXT2_NDIR_BLOCKS ){
  1245. errno = Enospace;
  1246. putbuf(ibuf);
  1247. return -1;
  1248. }
  1249. if( (baddr = inode_getblk(f, cur_block++)) == 0 ){
  1250. putbuf(ibuf);
  1251. return -1;
  1252. }
  1253. buf = getbuf(xf, baddr);
  1254. if( !buf ){
  1255. putbuf(ibuf);
  1256. return -1;
  1257. }
  1258. if( inode->i_size <= offset ){
  1259. de = (DirEntry *)buf->iobuf;
  1260. de->inode = 0;
  1261. de->rec_len = xf->block_size;
  1262. dirtybuf(buf);
  1263. inode->i_size = offset + xf->block_size;
  1264. dirtybuf(ibuf);
  1265. }else{
  1266. de = (DirEntry *)buf->iobuf;
  1267. }
  1268. }
  1269. if( de->inode != 0 && de->name_len == namelen &&
  1270. !strncmp(name, de->name, namelen) ){
  1271. errno = Eexist;
  1272. putbuf(ibuf); putbuf(buf);
  1273. return -1;
  1274. }
  1275. offset += de->rec_len;
  1276. if( (de->inode == 0 && de->rec_len >= rec_len) ||
  1277. (de->rec_len >= DIR_REC_LEN(de->name_len) + rec_len) ){
  1278. if( de->inode ){
  1279. de1 = (DirEntry *) ((char *)de + DIR_REC_LEN(de->name_len));
  1280. de1->rec_len = de->rec_len - DIR_REC_LEN(de->name_len);
  1281. de->rec_len = DIR_REC_LEN(de->name_len);
  1282. de = de1;
  1283. }
  1284. de->inode = inr;
  1285. de->name_len = namelen;
  1286. memcpy(de->name, name, namelen);
  1287. dirtybuf(buf);
  1288. putbuf(buf);
  1289. inode->i_mtime = inode->i_ctime = time(0);
  1290. dirtybuf(ibuf);
  1291. putbuf(ibuf);
  1292. return 0;
  1293. }
  1294. de = (DirEntry *)((char *)de + de->rec_len);
  1295. }
  1296. /* not reached */
  1297. }
  1298. int
  1299. unlink( Xfile *file )
  1300. {
  1301. Xfs *xf = file->xf;
  1302. Inode *dir;
  1303. int bg, b;
  1304. Inode *inode;
  1305. Iobuf *buf, *ibuf;
  1306. Ext2 ed, es, eb;
  1307. if( S_ISDIR(getmode(file)) && !empty_dir(file) ){
  1308. chat("non empty directory...");
  1309. errno = Eperm;
  1310. return -1;
  1311. }
  1312. es = getext2(xf, EXT2_SUPER, 0);
  1313. /* get dir inode */
  1314. if( file->pinbr >= es.u.sb->s_inodes_count ){
  1315. chat("inode number %d is too big...", file->pinbr);
  1316. putext2(es);
  1317. errno = Eintern;
  1318. return -1;
  1319. }
  1320. bg = (file->pinbr - 1) / xf->inodes_per_group;
  1321. if( bg >= xf->ngroups ){
  1322. chat("block group (%d) > groups count...", bg);
  1323. putext2(es);
  1324. errno = Eintern;
  1325. return -1;
  1326. }
  1327. ed = getext2(xf, EXT2_DESC, bg);
  1328. b = ed.u.gd->bg_inode_table +
  1329. (((file->pinbr-1) % xf->inodes_per_group) /
  1330. xf->inodes_per_block);
  1331. putext2(ed);
  1332. buf = getbuf(xf, b);
  1333. if( !buf ){
  1334. putext2(es);
  1335. return -1;
  1336. }
  1337. dir = ((struct Inode *) buf->iobuf) +
  1338. ((file->pinbr-1) % xf->inodes_per_block);
  1339. /* Clean dir entry */
  1340. if( delete_entry(xf, dir, file->inbr) < 0 ){
  1341. putbuf(buf);
  1342. putext2(es);
  1343. return -1;
  1344. }
  1345. if( S_ISDIR(getmode(file)) ){
  1346. dir->i_links_count--;
  1347. dirtybuf(buf);
  1348. }
  1349. putbuf(buf);
  1350. /* clean blocks */
  1351. ibuf = getbuf(xf, file->bufaddr);
  1352. if( !ibuf ){
  1353. putext2(es);
  1354. return -1;
  1355. }
  1356. inode = ((Inode *)ibuf->iobuf) + file->bufoffset;
  1357. if( !S_ISLNK(getmode(file)) ||
  1358. (S_ISLNK(getmode(file)) && (inode->i_size > EXT2_N_BLOCKS<<2)) )
  1359. if( free_block_inode(file) < 0 ){
  1360. chat("error while freeing blocks...");
  1361. putext2(es);
  1362. putbuf(ibuf);
  1363. return -1;
  1364. }
  1365. /* clean inode */
  1366. bg = (file->inbr -1) / xf->inodes_per_group;
  1367. b = (file->inbr -1) % xf->inodes_per_group;
  1368. eb = getext2(xf, EXT2_BINODE, bg);
  1369. clear_bit(b, eb.u.bmp);
  1370. dirtyext2(eb);
  1371. putext2(eb);
  1372. inode->i_dtime = time(0);
  1373. inode->i_links_count--;
  1374. if( S_ISDIR(getmode(file)) )
  1375. inode->i_links_count = 0;
  1376. es.u.sb->s_free_inodes_count++;
  1377. dirtyext2(es);
  1378. putext2(es);
  1379. ed = getext2(xf, EXT2_DESC, bg);
  1380. ed.u.gd->bg_free_inodes_count++;
  1381. if( S_ISDIR(getmode(file)) )
  1382. ed.u.gd->bg_used_dirs_count--;
  1383. dirtyext2(ed);
  1384. putext2(ed);
  1385. dirtybuf(ibuf);
  1386. putbuf(ibuf);
  1387. return 1;
  1388. }
  1389. int
  1390. empty_dir(Xfile *dir)
  1391. {
  1392. Xfs *xf = dir->xf;
  1393. int nblock;
  1394. uint offset, i,count;
  1395. DirEntry *de;
  1396. Inode *inode;
  1397. Iobuf *buf, *ibuf;
  1398. if( !S_ISDIR(getmode(dir)) )
  1399. return 0;
  1400. ibuf = getbuf(xf, dir->bufaddr);
  1401. if( !ibuf )
  1402. return -1;
  1403. inode = ((Inode *)ibuf->iobuf) + dir->bufoffset;
  1404. nblock = (inode->i_blocks * 512) / xf->block_size;
  1405. for(i=0, count=0 ; (i < nblock) && (i < EXT2_NDIR_BLOCKS) ; i++){
  1406. buf = getbuf(xf, inode->i_block[i]);
  1407. if( !buf ){
  1408. putbuf(ibuf);
  1409. return 0;
  1410. }
  1411. for(offset=0 ; offset < xf->block_size ; ){
  1412. de = (DirEntry *)(buf->iobuf + offset);
  1413. if(de->inode)
  1414. count++;
  1415. offset += de->rec_len;
  1416. }
  1417. putbuf(buf);
  1418. if( count > 2 ){
  1419. putbuf(ibuf);
  1420. return 0;
  1421. }
  1422. }
  1423. putbuf(ibuf);
  1424. return 1;
  1425. }
  1426. int
  1427. free_block_inode(Xfile *file)
  1428. {
  1429. Xfs *xf = file->xf;
  1430. int i, j, k;
  1431. uint32_t b, *y, *z;
  1432. uint *x;
  1433. int naddr;
  1434. Inode *inode;
  1435. Iobuf *buf, *buf1, *buf2, *ibuf;
  1436. ibuf = getbuf(xf, file->bufaddr);
  1437. if( !ibuf )
  1438. return -1;
  1439. inode = ((Inode *)ibuf->iobuf) + file->bufoffset;
  1440. for(i=0 ; i < EXT2_IND_BLOCK ; i++){
  1441. x = inode->i_block + i;
  1442. if( *x == 0 ){ putbuf(ibuf); return 0; }
  1443. free_block(xf, *x);
  1444. }
  1445. naddr = xf->addr_per_block;
  1446. /* indirect blocks */
  1447. if( (b=inode->i_block[EXT2_IND_BLOCK]) ){
  1448. buf = getbuf(xf, b);
  1449. if( !buf ){ putbuf(ibuf); return -1; }
  1450. for(i=0 ; i < naddr ; i++){
  1451. x = ((uint *)buf->iobuf) + i;
  1452. if( *x == 0 ) break;
  1453. free_block(xf, *x);
  1454. }
  1455. free_block(xf, b);
  1456. putbuf(buf);
  1457. }
  1458. /* double indirect block */
  1459. if( (b=inode->i_block[EXT2_DIND_BLOCK]) ){
  1460. buf = getbuf(xf, b);
  1461. if( !buf ){ putbuf(ibuf); return -1; }
  1462. for(i=0 ; i < naddr ; i++){
  1463. x = ((uint *)buf->iobuf) + i;
  1464. if( *x== 0 ) break;
  1465. buf1 = getbuf(xf, *x);
  1466. if( !buf1 ){ putbuf(buf); putbuf(ibuf); return -1; }
  1467. for(j=0 ; j < naddr ; j++){
  1468. y = ((uint32_t *)buf1->iobuf) + j;
  1469. if( *y == 0 ) break;
  1470. free_block(xf, *y);
  1471. }
  1472. free_block(xf, *x);
  1473. putbuf(buf1);
  1474. }
  1475. free_block(xf, b);
  1476. putbuf(buf);
  1477. }
  1478. /* triple indirect block */
  1479. if( (b=inode->i_block[EXT2_TIND_BLOCK]) ){
  1480. buf = getbuf(xf, b);
  1481. if( !buf ){ putbuf(ibuf); return -1; }
  1482. for(i=0 ; i < naddr ; i++){
  1483. x = ((uint *)buf->iobuf) + i;
  1484. if( *x == 0 ) break;
  1485. buf1 = getbuf(xf, *x);
  1486. if( !buf1 ){ putbuf(buf); putbuf(ibuf); return -1; }
  1487. for(j=0 ; j < naddr ; j++){
  1488. y = ((uint32_t *)buf1->iobuf) + j;
  1489. if( *y == 0 ) break;
  1490. buf2 = getbuf(xf, *y);
  1491. if( !buf2 ){ putbuf(buf); putbuf(buf1); putbuf(ibuf); return -1; }
  1492. for(k=0 ; k < naddr ; k++){
  1493. z = ((uint32_t *)buf2->iobuf) + k;
  1494. if( *z == 0 ) break;
  1495. free_block(xf, *z);
  1496. }
  1497. free_block(xf, *y);
  1498. putbuf(buf2);
  1499. }
  1500. free_block(xf, *x);
  1501. putbuf(buf1);
  1502. }
  1503. free_block(xf, b);
  1504. putbuf(buf);
  1505. }
  1506. putbuf(ibuf);
  1507. return 0;
  1508. }
  1509. void free_block( Xfs *xf, uint32_t block )
  1510. {
  1511. uint32_t bg;
  1512. Ext2 ed, es, eb;
  1513. es = getext2(xf, EXT2_SUPER, 0);
  1514. bg = (block - es.u.sb->s_first_data_block) / xf->blocks_per_group;
  1515. block = (block - es.u.sb->s_first_data_block) % xf->blocks_per_group;
  1516. eb = getext2(xf, EXT2_BBLOCK, bg);
  1517. clear_bit(block, eb.u.bmp);
  1518. dirtyext2(eb);
  1519. putext2(eb);
  1520. es.u.sb->s_free_blocks_count++;
  1521. dirtyext2(es);
  1522. putext2(es);
  1523. ed = getext2(xf, EXT2_DESC, bg);
  1524. ed.u.gd->bg_free_blocks_count++;
  1525. dirtyext2(ed);
  1526. putext2(ed);
  1527. }
  1528. int
  1529. delete_entry(Xfs *xf, Inode *inode, int inbr)
  1530. {
  1531. int nblock = (inode->i_blocks * 512) / xf->block_size;
  1532. uint offset, i;
  1533. DirEntry *de, *pde;
  1534. Iobuf *buf;
  1535. if( !S_ISDIR(inode->i_mode) )
  1536. return -1;
  1537. for(i=0 ; (i < nblock) && (i < EXT2_NDIR_BLOCKS) ; i++){
  1538. buf = getbuf(xf, inode->i_block[i]);
  1539. if( !buf )
  1540. return -1;
  1541. pde = 0;
  1542. for(offset=0 ; offset < xf->block_size ; ){
  1543. de = (DirEntry *)(buf->iobuf + offset);
  1544. if( de->inode == inbr ){
  1545. if( pde )
  1546. pde->rec_len += de->rec_len;
  1547. de->inode = 0;
  1548. dirtybuf(buf);
  1549. putbuf(buf);
  1550. return 1;
  1551. }
  1552. offset += de->rec_len;
  1553. pde = de;
  1554. }
  1555. putbuf(buf);
  1556. }
  1557. errno = Enonexist;
  1558. return -1;
  1559. }
  1560. int
  1561. truncfile(Xfile *f)
  1562. {
  1563. Inode *inode;
  1564. Iobuf *ibuf;
  1565. chat("trunc(fid=%d) ...", f->fid);
  1566. ibuf = getbuf(f->xf, f->bufaddr);
  1567. if( !ibuf )
  1568. return -1;
  1569. inode = ((Inode *)ibuf->iobuf) + f->bufoffset;
  1570. if( free_block_inode(f) < 0 ){
  1571. chat("error while freeing blocks...");
  1572. putbuf(ibuf);
  1573. return -1;
  1574. }
  1575. inode->i_atime = inode->i_mtime = time(0);
  1576. inode->i_blocks = 0;
  1577. inode->i_size = 0;
  1578. memset(inode->i_block, 0, EXT2_N_BLOCKS*sizeof(uint32_t));
  1579. dirtybuf(ibuf);
  1580. putbuf(ibuf);
  1581. chat("trunc ok...");
  1582. return 0;
  1583. }
  1584. int32_t
  1585. getmode(Xfile *f)
  1586. {
  1587. Iobuf *ibuf;
  1588. int32_t mode;
  1589. ibuf = getbuf(f->xf, f->bufaddr);
  1590. if( !ibuf )
  1591. return -1;
  1592. mode = (((Inode *)ibuf->iobuf) + f->bufoffset)->i_mode;
  1593. putbuf(ibuf);
  1594. return mode;
  1595. }
  1596. void
  1597. CleanSuper(Xfs *xf)
  1598. {
  1599. Ext2 es;
  1600. es = getext2(xf, EXT2_SUPER, 0);
  1601. es.u.sb->s_state = EXT2_VALID_FS;
  1602. dirtyext2(es);
  1603. putext2(es);
  1604. }
  1605. int
  1606. test_bit(int i, void *data)
  1607. {
  1608. char *pt = (char *)data;
  1609. return pt[i>>3] & (0x01 << (i&7));
  1610. }
  1611. int
  1612. set_bit(int i, void *data)
  1613. {
  1614. char *pt;
  1615. if( test_bit(i, data) )
  1616. return 1; /* bit already set !!! */
  1617. pt = (char *)data;
  1618. pt[i>>3] |= (0x01 << (i&7));
  1619. return 0;
  1620. }
  1621. int
  1622. clear_bit(int i, void *data)
  1623. {
  1624. char *pt;
  1625. if( !test_bit(i, data) )
  1626. return 1; /* bit already clear !!! */
  1627. pt = (char *)data;
  1628. pt[i>>3] &= ~(0x01 << (i&7));
  1629. return 0;
  1630. }
  1631. void *
  1632. memscan( void *data, int c, int count )
  1633. {
  1634. char *pt = (char *)data;
  1635. while( count ){
  1636. if( *pt == c )
  1637. return (void *)pt;
  1638. count--;
  1639. pt++;
  1640. }
  1641. return (void *)pt;
  1642. }
  1643. int
  1644. find_first_zero_bit( void *data, int count /* in byte */)
  1645. {
  1646. char *pt = (char *)data;
  1647. int n, i;
  1648. n = 0;
  1649. while( n < count ){
  1650. for(i=0 ; i < 8 ; i++)
  1651. if( !(*pt & (0x01 << (i&7))) )
  1652. return (n<<3) + i;
  1653. n++; pt++;
  1654. }
  1655. return n << 3;
  1656. }
  1657. int
  1658. find_next_zero_bit( void *data, int count /* in byte */, int where)
  1659. {
  1660. char *pt = (((char *)data) + (where >> 3));
  1661. int n, i;
  1662. n = where >> 3;
  1663. i = where & 7;
  1664. while( n < count ){
  1665. for(; i < 8 ; i++)
  1666. if( !(*pt & (0x01 << (i&7))) )
  1667. return (n<<3) + i;
  1668. n++; pt++; i=0;
  1669. }
  1670. return n << 3;
  1671. }
  1672. int
  1673. ffz( int x )
  1674. {
  1675. int c = 0;
  1676. while( x&1 ){
  1677. c++;
  1678. x >>= 1;
  1679. }
  1680. return c;
  1681. }