ext2subs.c 38 KB

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