ext2subs.c 38 KB

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