ext2subs.c 38 KB

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