inode.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. /*
  2. * inode.c
  3. *
  4. * PURPOSE
  5. * Inode handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/04/98 dgb Added rudimentary directory functions
  20. * 10/07/98 Fully working udf_block_map! It works!
  21. * 11/25/98 bmap altered to better support extents
  22. * 12/06/98 blf partition support in udf_iget, udf_block_map
  23. * and udf_read_inode
  24. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  25. * block boundaries (which is not actually allowed)
  26. * 12/20/98 added support for strategy 4096
  27. * 03/07/99 rewrote udf_block_map (again)
  28. * New funcs, inode_bmap, udf_next_aext
  29. * 04/19/99 Support for writing device EA's for major/minor #
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/mm.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/writeback.h>
  38. #include <linux/slab.h>
  39. #include <linux/crc-itu-t.h>
  40. #include "udf_i.h"
  41. #include "udf_sb.h"
  42. MODULE_AUTHOR("Ben Fennema");
  43. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  44. MODULE_LICENSE("GPL");
  45. #define EXTENT_MERGE_SIZE 5
  46. static mode_t udf_convert_permissions(struct fileEntry *);
  47. static int udf_update_inode(struct inode *, int);
  48. static void udf_fill_inode(struct inode *, struct buffer_head *);
  49. static int udf_alloc_i_data(struct inode *inode, size_t size);
  50. static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
  51. sector_t *, int *);
  52. static int8_t udf_insert_aext(struct inode *, struct extent_position,
  53. struct kernel_lb_addr, uint32_t);
  54. static void udf_split_extents(struct inode *, int *, int, int,
  55. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  56. static void udf_prealloc_extents(struct inode *, int, int,
  57. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  58. static void udf_merge_extents(struct inode *,
  59. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  60. static void udf_update_extents(struct inode *,
  61. struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
  62. struct extent_position *);
  63. static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  64. void udf_delete_inode(struct inode *inode)
  65. {
  66. truncate_inode_pages(&inode->i_data, 0);
  67. if (is_bad_inode(inode))
  68. goto no_delete;
  69. inode->i_size = 0;
  70. udf_truncate(inode);
  71. lock_kernel();
  72. udf_update_inode(inode, IS_SYNC(inode));
  73. udf_free_inode(inode);
  74. unlock_kernel();
  75. return;
  76. no_delete:
  77. clear_inode(inode);
  78. }
  79. /*
  80. * If we are going to release inode from memory, we truncate last inode extent
  81. * to proper length. We could use drop_inode() but it's called under inode_lock
  82. * and thus we cannot mark inode dirty there. We use clear_inode() but we have
  83. * to make sure to write inode as it's not written automatically.
  84. */
  85. void udf_clear_inode(struct inode *inode)
  86. {
  87. struct udf_inode_info *iinfo;
  88. if (!(inode->i_sb->s_flags & MS_RDONLY)) {
  89. lock_kernel();
  90. udf_truncate_tail_extent(inode);
  91. unlock_kernel();
  92. write_inode_now(inode, 0);
  93. invalidate_inode_buffers(inode);
  94. }
  95. iinfo = UDF_I(inode);
  96. kfree(iinfo->i_ext.i_data);
  97. iinfo->i_ext.i_data = NULL;
  98. }
  99. static int udf_writepage(struct page *page, struct writeback_control *wbc)
  100. {
  101. return block_write_full_page(page, udf_get_block, wbc);
  102. }
  103. static int udf_readpage(struct file *file, struct page *page)
  104. {
  105. return block_read_full_page(page, udf_get_block);
  106. }
  107. static int udf_write_begin(struct file *file, struct address_space *mapping,
  108. loff_t pos, unsigned len, unsigned flags,
  109. struct page **pagep, void **fsdata)
  110. {
  111. *pagep = NULL;
  112. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  113. udf_get_block);
  114. }
  115. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  116. {
  117. return generic_block_bmap(mapping, block, udf_get_block);
  118. }
  119. const struct address_space_operations udf_aops = {
  120. .readpage = udf_readpage,
  121. .writepage = udf_writepage,
  122. .sync_page = block_sync_page,
  123. .write_begin = udf_write_begin,
  124. .write_end = generic_write_end,
  125. .bmap = udf_bmap,
  126. };
  127. void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
  128. {
  129. struct page *page;
  130. char *kaddr;
  131. struct udf_inode_info *iinfo = UDF_I(inode);
  132. struct writeback_control udf_wbc = {
  133. .sync_mode = WB_SYNC_NONE,
  134. .nr_to_write = 1,
  135. };
  136. /* from now on we have normal address_space methods */
  137. inode->i_data.a_ops = &udf_aops;
  138. if (!iinfo->i_lenAlloc) {
  139. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  140. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  141. else
  142. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  143. mark_inode_dirty(inode);
  144. return;
  145. }
  146. page = grab_cache_page(inode->i_mapping, 0);
  147. BUG_ON(!PageLocked(page));
  148. if (!PageUptodate(page)) {
  149. kaddr = kmap(page);
  150. memset(kaddr + iinfo->i_lenAlloc, 0x00,
  151. PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
  152. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
  153. iinfo->i_lenAlloc);
  154. flush_dcache_page(page);
  155. SetPageUptodate(page);
  156. kunmap(page);
  157. }
  158. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
  159. iinfo->i_lenAlloc);
  160. iinfo->i_lenAlloc = 0;
  161. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  162. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  163. else
  164. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  165. inode->i_data.a_ops->writepage(page, &udf_wbc);
  166. page_cache_release(page);
  167. mark_inode_dirty(inode);
  168. }
  169. struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
  170. int *err)
  171. {
  172. int newblock;
  173. struct buffer_head *dbh = NULL;
  174. struct kernel_lb_addr eloc;
  175. uint32_t elen;
  176. uint8_t alloctype;
  177. struct extent_position epos;
  178. struct udf_fileident_bh sfibh, dfibh;
  179. loff_t f_pos = udf_ext0_offset(inode);
  180. int size = udf_ext0_offset(inode) + inode->i_size;
  181. struct fileIdentDesc cfi, *sfi, *dfi;
  182. struct udf_inode_info *iinfo = UDF_I(inode);
  183. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  184. alloctype = ICBTAG_FLAG_AD_SHORT;
  185. else
  186. alloctype = ICBTAG_FLAG_AD_LONG;
  187. if (!inode->i_size) {
  188. iinfo->i_alloc_type = alloctype;
  189. mark_inode_dirty(inode);
  190. return NULL;
  191. }
  192. /* alloc block, and copy data to it */
  193. *block = udf_new_block(inode->i_sb, inode,
  194. iinfo->i_location.partitionReferenceNum,
  195. iinfo->i_location.logicalBlockNum, err);
  196. if (!(*block))
  197. return NULL;
  198. newblock = udf_get_pblock(inode->i_sb, *block,
  199. iinfo->i_location.partitionReferenceNum,
  200. 0);
  201. if (!newblock)
  202. return NULL;
  203. dbh = udf_tgetblk(inode->i_sb, newblock);
  204. if (!dbh)
  205. return NULL;
  206. lock_buffer(dbh);
  207. memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
  208. set_buffer_uptodate(dbh);
  209. unlock_buffer(dbh);
  210. mark_buffer_dirty_inode(dbh, inode);
  211. sfibh.soffset = sfibh.eoffset =
  212. f_pos & (inode->i_sb->s_blocksize - 1);
  213. sfibh.sbh = sfibh.ebh = NULL;
  214. dfibh.soffset = dfibh.eoffset = 0;
  215. dfibh.sbh = dfibh.ebh = dbh;
  216. while (f_pos < size) {
  217. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  218. sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
  219. NULL, NULL, NULL);
  220. if (!sfi) {
  221. brelse(dbh);
  222. return NULL;
  223. }
  224. iinfo->i_alloc_type = alloctype;
  225. sfi->descTag.tagLocation = cpu_to_le32(*block);
  226. dfibh.soffset = dfibh.eoffset;
  227. dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
  228. dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
  229. if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
  230. sfi->fileIdent +
  231. le16_to_cpu(sfi->lengthOfImpUse))) {
  232. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  233. brelse(dbh);
  234. return NULL;
  235. }
  236. }
  237. mark_buffer_dirty_inode(dbh, inode);
  238. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
  239. iinfo->i_lenAlloc);
  240. iinfo->i_lenAlloc = 0;
  241. eloc.logicalBlockNum = *block;
  242. eloc.partitionReferenceNum =
  243. iinfo->i_location.partitionReferenceNum;
  244. elen = inode->i_sb->s_blocksize;
  245. iinfo->i_lenExtents = elen;
  246. epos.bh = NULL;
  247. epos.block = iinfo->i_location;
  248. epos.offset = udf_file_entry_alloc_offset(inode);
  249. udf_add_aext(inode, &epos, &eloc, elen, 0);
  250. /* UniqueID stuff */
  251. brelse(epos.bh);
  252. mark_inode_dirty(inode);
  253. return dbh;
  254. }
  255. static int udf_get_block(struct inode *inode, sector_t block,
  256. struct buffer_head *bh_result, int create)
  257. {
  258. int err, new;
  259. struct buffer_head *bh;
  260. sector_t phys = 0;
  261. struct udf_inode_info *iinfo;
  262. if (!create) {
  263. phys = udf_block_map(inode, block);
  264. if (phys)
  265. map_bh(bh_result, inode->i_sb, phys);
  266. return 0;
  267. }
  268. err = -EIO;
  269. new = 0;
  270. bh = NULL;
  271. lock_kernel();
  272. iinfo = UDF_I(inode);
  273. if (block == iinfo->i_next_alloc_block + 1) {
  274. iinfo->i_next_alloc_block++;
  275. iinfo->i_next_alloc_goal++;
  276. }
  277. err = 0;
  278. bh = inode_getblk(inode, block, &err, &phys, &new);
  279. BUG_ON(bh);
  280. if (err)
  281. goto abort;
  282. BUG_ON(!phys);
  283. if (new)
  284. set_buffer_new(bh_result);
  285. map_bh(bh_result, inode->i_sb, phys);
  286. abort:
  287. unlock_kernel();
  288. return err;
  289. }
  290. static struct buffer_head *udf_getblk(struct inode *inode, long block,
  291. int create, int *err)
  292. {
  293. struct buffer_head *bh;
  294. struct buffer_head dummy;
  295. dummy.b_state = 0;
  296. dummy.b_blocknr = -1000;
  297. *err = udf_get_block(inode, block, &dummy, create);
  298. if (!*err && buffer_mapped(&dummy)) {
  299. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  300. if (buffer_new(&dummy)) {
  301. lock_buffer(bh);
  302. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  303. set_buffer_uptodate(bh);
  304. unlock_buffer(bh);
  305. mark_buffer_dirty_inode(bh, inode);
  306. }
  307. return bh;
  308. }
  309. return NULL;
  310. }
  311. /* Extend the file by 'blocks' blocks, return the number of extents added */
  312. int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
  313. struct kernel_long_ad *last_ext, sector_t blocks)
  314. {
  315. sector_t add;
  316. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  317. struct super_block *sb = inode->i_sb;
  318. struct kernel_lb_addr prealloc_loc = {};
  319. int prealloc_len = 0;
  320. struct udf_inode_info *iinfo;
  321. /* The previous extent is fake and we should not extend by anything
  322. * - there's nothing to do... */
  323. if (!blocks && fake)
  324. return 0;
  325. iinfo = UDF_I(inode);
  326. /* Round the last extent up to a multiple of block size */
  327. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  328. last_ext->extLength =
  329. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  330. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  331. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  332. iinfo->i_lenExtents =
  333. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  334. ~(sb->s_blocksize - 1);
  335. }
  336. /* Last extent are just preallocated blocks? */
  337. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  338. EXT_NOT_RECORDED_ALLOCATED) {
  339. /* Save the extent so that we can reattach it to the end */
  340. prealloc_loc = last_ext->extLocation;
  341. prealloc_len = last_ext->extLength;
  342. /* Mark the extent as a hole */
  343. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  344. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  345. last_ext->extLocation.logicalBlockNum = 0;
  346. last_ext->extLocation.partitionReferenceNum = 0;
  347. }
  348. /* Can we merge with the previous extent? */
  349. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  350. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  351. add = ((1 << 30) - sb->s_blocksize -
  352. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  353. sb->s_blocksize_bits;
  354. if (add > blocks)
  355. add = blocks;
  356. blocks -= add;
  357. last_ext->extLength += add << sb->s_blocksize_bits;
  358. }
  359. if (fake) {
  360. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  361. last_ext->extLength, 1);
  362. count++;
  363. } else
  364. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  365. last_ext->extLength, 1);
  366. /* Managed to do everything necessary? */
  367. if (!blocks)
  368. goto out;
  369. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  370. last_ext->extLocation.logicalBlockNum = 0;
  371. last_ext->extLocation.partitionReferenceNum = 0;
  372. add = (1 << (30-sb->s_blocksize_bits)) - 1;
  373. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  374. (add << sb->s_blocksize_bits);
  375. /* Create enough extents to cover the whole hole */
  376. while (blocks > add) {
  377. blocks -= add;
  378. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  379. last_ext->extLength, 1) == -1)
  380. return -1;
  381. count++;
  382. }
  383. if (blocks) {
  384. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  385. (blocks << sb->s_blocksize_bits);
  386. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  387. last_ext->extLength, 1) == -1)
  388. return -1;
  389. count++;
  390. }
  391. out:
  392. /* Do we have some preallocated blocks saved? */
  393. if (prealloc_len) {
  394. if (udf_add_aext(inode, last_pos, &prealloc_loc,
  395. prealloc_len, 1) == -1)
  396. return -1;
  397. last_ext->extLocation = prealloc_loc;
  398. last_ext->extLength = prealloc_len;
  399. count++;
  400. }
  401. /* last_pos should point to the last written extent... */
  402. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  403. last_pos->offset -= sizeof(struct short_ad);
  404. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  405. last_pos->offset -= sizeof(struct long_ad);
  406. else
  407. return -1;
  408. return count;
  409. }
  410. static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
  411. int *err, sector_t *phys, int *new)
  412. {
  413. static sector_t last_block;
  414. struct buffer_head *result = NULL;
  415. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  416. struct extent_position prev_epos, cur_epos, next_epos;
  417. int count = 0, startnum = 0, endnum = 0;
  418. uint32_t elen = 0, tmpelen;
  419. struct kernel_lb_addr eloc, tmpeloc;
  420. int c = 1;
  421. loff_t lbcount = 0, b_off = 0;
  422. uint32_t newblocknum, newblock;
  423. sector_t offset = 0;
  424. int8_t etype;
  425. struct udf_inode_info *iinfo = UDF_I(inode);
  426. int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  427. int lastblock = 0;
  428. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  429. prev_epos.block = iinfo->i_location;
  430. prev_epos.bh = NULL;
  431. cur_epos = next_epos = prev_epos;
  432. b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
  433. /* find the extent which contains the block we are looking for.
  434. alternate between laarr[0] and laarr[1] for locations of the
  435. current extent, and the previous extent */
  436. do {
  437. if (prev_epos.bh != cur_epos.bh) {
  438. brelse(prev_epos.bh);
  439. get_bh(cur_epos.bh);
  440. prev_epos.bh = cur_epos.bh;
  441. }
  442. if (cur_epos.bh != next_epos.bh) {
  443. brelse(cur_epos.bh);
  444. get_bh(next_epos.bh);
  445. cur_epos.bh = next_epos.bh;
  446. }
  447. lbcount += elen;
  448. prev_epos.block = cur_epos.block;
  449. cur_epos.block = next_epos.block;
  450. prev_epos.offset = cur_epos.offset;
  451. cur_epos.offset = next_epos.offset;
  452. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
  453. if (etype == -1)
  454. break;
  455. c = !c;
  456. laarr[c].extLength = (etype << 30) | elen;
  457. laarr[c].extLocation = eloc;
  458. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  459. pgoal = eloc.logicalBlockNum +
  460. ((elen + inode->i_sb->s_blocksize - 1) >>
  461. inode->i_sb->s_blocksize_bits);
  462. count++;
  463. } while (lbcount + elen <= b_off);
  464. b_off -= lbcount;
  465. offset = b_off >> inode->i_sb->s_blocksize_bits;
  466. /*
  467. * Move prev_epos and cur_epos into indirect extent if we are at
  468. * the pointer to it
  469. */
  470. udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
  471. udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
  472. /* if the extent is allocated and recorded, return the block
  473. if the extent is not a multiple of the blocksize, round up */
  474. if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  475. if (elen & (inode->i_sb->s_blocksize - 1)) {
  476. elen = EXT_RECORDED_ALLOCATED |
  477. ((elen + inode->i_sb->s_blocksize - 1) &
  478. ~(inode->i_sb->s_blocksize - 1));
  479. etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  480. }
  481. brelse(prev_epos.bh);
  482. brelse(cur_epos.bh);
  483. brelse(next_epos.bh);
  484. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  485. *phys = newblock;
  486. return NULL;
  487. }
  488. last_block = block;
  489. /* Are we beyond EOF? */
  490. if (etype == -1) {
  491. int ret;
  492. if (count) {
  493. if (c)
  494. laarr[0] = laarr[1];
  495. startnum = 1;
  496. } else {
  497. /* Create a fake extent when there's not one */
  498. memset(&laarr[0].extLocation, 0x00,
  499. sizeof(struct kernel_lb_addr));
  500. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  501. /* Will udf_extend_file() create real extent from
  502. a fake one? */
  503. startnum = (offset > 0);
  504. }
  505. /* Create extents for the hole between EOF and offset */
  506. ret = udf_extend_file(inode, &prev_epos, laarr, offset);
  507. if (ret == -1) {
  508. brelse(prev_epos.bh);
  509. brelse(cur_epos.bh);
  510. brelse(next_epos.bh);
  511. /* We don't really know the error here so we just make
  512. * something up */
  513. *err = -ENOSPC;
  514. return NULL;
  515. }
  516. c = 0;
  517. offset = 0;
  518. count += ret;
  519. /* We are not covered by a preallocated extent? */
  520. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  521. EXT_NOT_RECORDED_ALLOCATED) {
  522. /* Is there any real extent? - otherwise we overwrite
  523. * the fake one... */
  524. if (count)
  525. c = !c;
  526. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  527. inode->i_sb->s_blocksize;
  528. memset(&laarr[c].extLocation, 0x00,
  529. sizeof(struct kernel_lb_addr));
  530. count++;
  531. endnum++;
  532. }
  533. endnum = c + 1;
  534. lastblock = 1;
  535. } else {
  536. endnum = startnum = ((count > 2) ? 2 : count);
  537. /* if the current extent is in position 0,
  538. swap it with the previous */
  539. if (!c && count != 1) {
  540. laarr[2] = laarr[0];
  541. laarr[0] = laarr[1];
  542. laarr[1] = laarr[2];
  543. c = 1;
  544. }
  545. /* if the current block is located in an extent,
  546. read the next extent */
  547. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  548. if (etype != -1) {
  549. laarr[c + 1].extLength = (etype << 30) | elen;
  550. laarr[c + 1].extLocation = eloc;
  551. count++;
  552. startnum++;
  553. endnum++;
  554. } else
  555. lastblock = 1;
  556. }
  557. /* if the current extent is not recorded but allocated, get the
  558. * block in the extent corresponding to the requested block */
  559. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  560. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  561. else { /* otherwise, allocate a new block */
  562. if (iinfo->i_next_alloc_block == block)
  563. goal = iinfo->i_next_alloc_goal;
  564. if (!goal) {
  565. if (!(goal = pgoal)) /* XXX: what was intended here? */
  566. goal = iinfo->i_location.logicalBlockNum + 1;
  567. }
  568. newblocknum = udf_new_block(inode->i_sb, inode,
  569. iinfo->i_location.partitionReferenceNum,
  570. goal, err);
  571. if (!newblocknum) {
  572. brelse(prev_epos.bh);
  573. *err = -ENOSPC;
  574. return NULL;
  575. }
  576. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  577. }
  578. /* if the extent the requsted block is located in contains multiple
  579. * blocks, split the extent into at most three extents. blocks prior
  580. * to requested block, requested block, and blocks after requested
  581. * block */
  582. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  583. #ifdef UDF_PREALLOCATE
  584. /* We preallocate blocks only for regular files. It also makes sense
  585. * for directories but there's a problem when to drop the
  586. * preallocation. We might use some delayed work for that but I feel
  587. * it's overengineering for a filesystem like UDF. */
  588. if (S_ISREG(inode->i_mode))
  589. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  590. #endif
  591. /* merge any continuous blocks in laarr */
  592. udf_merge_extents(inode, laarr, &endnum);
  593. /* write back the new extents, inserting new extents if the new number
  594. * of extents is greater than the old number, and deleting extents if
  595. * the new number of extents is less than the old number */
  596. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  597. brelse(prev_epos.bh);
  598. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  599. iinfo->i_location.partitionReferenceNum, 0);
  600. if (!newblock)
  601. return NULL;
  602. *phys = newblock;
  603. *err = 0;
  604. *new = 1;
  605. iinfo->i_next_alloc_block = block;
  606. iinfo->i_next_alloc_goal = newblocknum;
  607. inode->i_ctime = current_fs_time(inode->i_sb);
  608. if (IS_SYNC(inode))
  609. udf_sync_inode(inode);
  610. else
  611. mark_inode_dirty(inode);
  612. return result;
  613. }
  614. static void udf_split_extents(struct inode *inode, int *c, int offset,
  615. int newblocknum,
  616. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  617. int *endnum)
  618. {
  619. unsigned long blocksize = inode->i_sb->s_blocksize;
  620. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  621. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  622. (laarr[*c].extLength >> 30) ==
  623. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  624. int curr = *c;
  625. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  626. blocksize - 1) >> blocksize_bits;
  627. int8_t etype = (laarr[curr].extLength >> 30);
  628. if (blen == 1)
  629. ;
  630. else if (!offset || blen == offset + 1) {
  631. laarr[curr + 2] = laarr[curr + 1];
  632. laarr[curr + 1] = laarr[curr];
  633. } else {
  634. laarr[curr + 3] = laarr[curr + 1];
  635. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  636. }
  637. if (offset) {
  638. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  639. udf_free_blocks(inode->i_sb, inode,
  640. &laarr[curr].extLocation,
  641. 0, offset);
  642. laarr[curr].extLength =
  643. EXT_NOT_RECORDED_NOT_ALLOCATED |
  644. (offset << blocksize_bits);
  645. laarr[curr].extLocation.logicalBlockNum = 0;
  646. laarr[curr].extLocation.
  647. partitionReferenceNum = 0;
  648. } else
  649. laarr[curr].extLength = (etype << 30) |
  650. (offset << blocksize_bits);
  651. curr++;
  652. (*c)++;
  653. (*endnum)++;
  654. }
  655. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  656. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  657. laarr[curr].extLocation.partitionReferenceNum =
  658. UDF_I(inode)->i_location.partitionReferenceNum;
  659. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  660. blocksize;
  661. curr++;
  662. if (blen != offset + 1) {
  663. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  664. laarr[curr].extLocation.logicalBlockNum +=
  665. offset + 1;
  666. laarr[curr].extLength = (etype << 30) |
  667. ((blen - (offset + 1)) << blocksize_bits);
  668. curr++;
  669. (*endnum)++;
  670. }
  671. }
  672. }
  673. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  674. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  675. int *endnum)
  676. {
  677. int start, length = 0, currlength = 0, i;
  678. if (*endnum >= (c + 1)) {
  679. if (!lastblock)
  680. return;
  681. else
  682. start = c;
  683. } else {
  684. if ((laarr[c + 1].extLength >> 30) ==
  685. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  686. start = c + 1;
  687. length = currlength =
  688. (((laarr[c + 1].extLength &
  689. UDF_EXTENT_LENGTH_MASK) +
  690. inode->i_sb->s_blocksize - 1) >>
  691. inode->i_sb->s_blocksize_bits);
  692. } else
  693. start = c;
  694. }
  695. for (i = start + 1; i <= *endnum; i++) {
  696. if (i == *endnum) {
  697. if (lastblock)
  698. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  699. } else if ((laarr[i].extLength >> 30) ==
  700. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  701. length += (((laarr[i].extLength &
  702. UDF_EXTENT_LENGTH_MASK) +
  703. inode->i_sb->s_blocksize - 1) >>
  704. inode->i_sb->s_blocksize_bits);
  705. } else
  706. break;
  707. }
  708. if (length) {
  709. int next = laarr[start].extLocation.logicalBlockNum +
  710. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  711. inode->i_sb->s_blocksize - 1) >>
  712. inode->i_sb->s_blocksize_bits);
  713. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  714. laarr[start].extLocation.partitionReferenceNum,
  715. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  716. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  717. currlength);
  718. if (numalloc) {
  719. if (start == (c + 1))
  720. laarr[start].extLength +=
  721. (numalloc <<
  722. inode->i_sb->s_blocksize_bits);
  723. else {
  724. memmove(&laarr[c + 2], &laarr[c + 1],
  725. sizeof(struct long_ad) * (*endnum - (c + 1)));
  726. (*endnum)++;
  727. laarr[c + 1].extLocation.logicalBlockNum = next;
  728. laarr[c + 1].extLocation.partitionReferenceNum =
  729. laarr[c].extLocation.
  730. partitionReferenceNum;
  731. laarr[c + 1].extLength =
  732. EXT_NOT_RECORDED_ALLOCATED |
  733. (numalloc <<
  734. inode->i_sb->s_blocksize_bits);
  735. start = c + 1;
  736. }
  737. for (i = start + 1; numalloc && i < *endnum; i++) {
  738. int elen = ((laarr[i].extLength &
  739. UDF_EXTENT_LENGTH_MASK) +
  740. inode->i_sb->s_blocksize - 1) >>
  741. inode->i_sb->s_blocksize_bits;
  742. if (elen > numalloc) {
  743. laarr[i].extLength -=
  744. (numalloc <<
  745. inode->i_sb->s_blocksize_bits);
  746. numalloc = 0;
  747. } else {
  748. numalloc -= elen;
  749. if (*endnum > (i + 1))
  750. memmove(&laarr[i],
  751. &laarr[i + 1],
  752. sizeof(struct long_ad) *
  753. (*endnum - (i + 1)));
  754. i--;
  755. (*endnum)--;
  756. }
  757. }
  758. UDF_I(inode)->i_lenExtents +=
  759. numalloc << inode->i_sb->s_blocksize_bits;
  760. }
  761. }
  762. }
  763. static void udf_merge_extents(struct inode *inode,
  764. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  765. int *endnum)
  766. {
  767. int i;
  768. unsigned long blocksize = inode->i_sb->s_blocksize;
  769. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  770. for (i = 0; i < (*endnum - 1); i++) {
  771. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  772. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  773. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  774. (((li->extLength >> 30) ==
  775. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  776. ((lip1->extLocation.logicalBlockNum -
  777. li->extLocation.logicalBlockNum) ==
  778. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  779. blocksize - 1) >> blocksize_bits)))) {
  780. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  781. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  782. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  783. lip1->extLength = (lip1->extLength -
  784. (li->extLength &
  785. UDF_EXTENT_LENGTH_MASK) +
  786. UDF_EXTENT_LENGTH_MASK) &
  787. ~(blocksize - 1);
  788. li->extLength = (li->extLength &
  789. UDF_EXTENT_FLAG_MASK) +
  790. (UDF_EXTENT_LENGTH_MASK + 1) -
  791. blocksize;
  792. lip1->extLocation.logicalBlockNum =
  793. li->extLocation.logicalBlockNum +
  794. ((li->extLength &
  795. UDF_EXTENT_LENGTH_MASK) >>
  796. blocksize_bits);
  797. } else {
  798. li->extLength = lip1->extLength +
  799. (((li->extLength &
  800. UDF_EXTENT_LENGTH_MASK) +
  801. blocksize - 1) & ~(blocksize - 1));
  802. if (*endnum > (i + 2))
  803. memmove(&laarr[i + 1], &laarr[i + 2],
  804. sizeof(struct long_ad) *
  805. (*endnum - (i + 2)));
  806. i--;
  807. (*endnum)--;
  808. }
  809. } else if (((li->extLength >> 30) ==
  810. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  811. ((lip1->extLength >> 30) ==
  812. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  813. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  814. ((li->extLength &
  815. UDF_EXTENT_LENGTH_MASK) +
  816. blocksize - 1) >> blocksize_bits);
  817. li->extLocation.logicalBlockNum = 0;
  818. li->extLocation.partitionReferenceNum = 0;
  819. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  820. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  821. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  822. lip1->extLength = (lip1->extLength -
  823. (li->extLength &
  824. UDF_EXTENT_LENGTH_MASK) +
  825. UDF_EXTENT_LENGTH_MASK) &
  826. ~(blocksize - 1);
  827. li->extLength = (li->extLength &
  828. UDF_EXTENT_FLAG_MASK) +
  829. (UDF_EXTENT_LENGTH_MASK + 1) -
  830. blocksize;
  831. } else {
  832. li->extLength = lip1->extLength +
  833. (((li->extLength &
  834. UDF_EXTENT_LENGTH_MASK) +
  835. blocksize - 1) & ~(blocksize - 1));
  836. if (*endnum > (i + 2))
  837. memmove(&laarr[i + 1], &laarr[i + 2],
  838. sizeof(struct long_ad) *
  839. (*endnum - (i + 2)));
  840. i--;
  841. (*endnum)--;
  842. }
  843. } else if ((li->extLength >> 30) ==
  844. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  845. udf_free_blocks(inode->i_sb, inode,
  846. &li->extLocation, 0,
  847. ((li->extLength &
  848. UDF_EXTENT_LENGTH_MASK) +
  849. blocksize - 1) >> blocksize_bits);
  850. li->extLocation.logicalBlockNum = 0;
  851. li->extLocation.partitionReferenceNum = 0;
  852. li->extLength = (li->extLength &
  853. UDF_EXTENT_LENGTH_MASK) |
  854. EXT_NOT_RECORDED_NOT_ALLOCATED;
  855. }
  856. }
  857. }
  858. static void udf_update_extents(struct inode *inode,
  859. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  860. int startnum, int endnum,
  861. struct extent_position *epos)
  862. {
  863. int start = 0, i;
  864. struct kernel_lb_addr tmploc;
  865. uint32_t tmplen;
  866. if (startnum > endnum) {
  867. for (i = 0; i < (startnum - endnum); i++)
  868. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  869. laarr[i].extLength);
  870. } else if (startnum < endnum) {
  871. for (i = 0; i < (endnum - startnum); i++) {
  872. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  873. laarr[i].extLength);
  874. udf_next_aext(inode, epos, &laarr[i].extLocation,
  875. &laarr[i].extLength, 1);
  876. start++;
  877. }
  878. }
  879. for (i = start; i < endnum; i++) {
  880. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  881. udf_write_aext(inode, epos, &laarr[i].extLocation,
  882. laarr[i].extLength, 1);
  883. }
  884. }
  885. struct buffer_head *udf_bread(struct inode *inode, int block,
  886. int create, int *err)
  887. {
  888. struct buffer_head *bh = NULL;
  889. bh = udf_getblk(inode, block, create, err);
  890. if (!bh)
  891. return NULL;
  892. if (buffer_uptodate(bh))
  893. return bh;
  894. ll_rw_block(READ, 1, &bh);
  895. wait_on_buffer(bh);
  896. if (buffer_uptodate(bh))
  897. return bh;
  898. brelse(bh);
  899. *err = -EIO;
  900. return NULL;
  901. }
  902. void udf_truncate(struct inode *inode)
  903. {
  904. int offset;
  905. int err;
  906. struct udf_inode_info *iinfo;
  907. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  908. S_ISLNK(inode->i_mode)))
  909. return;
  910. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  911. return;
  912. lock_kernel();
  913. iinfo = UDF_I(inode);
  914. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  915. if (inode->i_sb->s_blocksize <
  916. (udf_file_entry_alloc_offset(inode) +
  917. inode->i_size)) {
  918. udf_expand_file_adinicb(inode, inode->i_size, &err);
  919. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  920. inode->i_size = iinfo->i_lenAlloc;
  921. unlock_kernel();
  922. return;
  923. } else
  924. udf_truncate_extents(inode);
  925. } else {
  926. offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
  927. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
  928. 0x00, inode->i_sb->s_blocksize -
  929. offset - udf_file_entry_alloc_offset(inode));
  930. iinfo->i_lenAlloc = inode->i_size;
  931. }
  932. } else {
  933. block_truncate_page(inode->i_mapping, inode->i_size,
  934. udf_get_block);
  935. udf_truncate_extents(inode);
  936. }
  937. inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
  938. if (IS_SYNC(inode))
  939. udf_sync_inode(inode);
  940. else
  941. mark_inode_dirty(inode);
  942. unlock_kernel();
  943. }
  944. static void __udf_read_inode(struct inode *inode)
  945. {
  946. struct buffer_head *bh = NULL;
  947. struct fileEntry *fe;
  948. uint16_t ident;
  949. struct udf_inode_info *iinfo = UDF_I(inode);
  950. /*
  951. * Set defaults, but the inode is still incomplete!
  952. * Note: get_new_inode() sets the following on a new inode:
  953. * i_sb = sb
  954. * i_no = ino
  955. * i_flags = sb->s_flags
  956. * i_state = 0
  957. * clean_inode(): zero fills and sets
  958. * i_count = 1
  959. * i_nlink = 1
  960. * i_op = NULL;
  961. */
  962. bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
  963. if (!bh) {
  964. printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
  965. inode->i_ino);
  966. make_bad_inode(inode);
  967. return;
  968. }
  969. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  970. ident != TAG_IDENT_USE) {
  971. printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
  972. "failed ident=%d\n", inode->i_ino, ident);
  973. brelse(bh);
  974. make_bad_inode(inode);
  975. return;
  976. }
  977. fe = (struct fileEntry *)bh->b_data;
  978. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  979. struct buffer_head *ibh;
  980. ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
  981. &ident);
  982. if (ident == TAG_IDENT_IE && ibh) {
  983. struct buffer_head *nbh = NULL;
  984. struct kernel_lb_addr loc;
  985. struct indirectEntry *ie;
  986. ie = (struct indirectEntry *)ibh->b_data;
  987. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  988. if (ie->indirectICB.extLength &&
  989. (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
  990. &ident))) {
  991. if (ident == TAG_IDENT_FE ||
  992. ident == TAG_IDENT_EFE) {
  993. memcpy(&iinfo->i_location,
  994. &loc,
  995. sizeof(struct kernel_lb_addr));
  996. brelse(bh);
  997. brelse(ibh);
  998. brelse(nbh);
  999. __udf_read_inode(inode);
  1000. return;
  1001. }
  1002. brelse(nbh);
  1003. }
  1004. }
  1005. brelse(ibh);
  1006. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1007. printk(KERN_ERR "udf: unsupported strategy type: %d\n",
  1008. le16_to_cpu(fe->icbTag.strategyType));
  1009. brelse(bh);
  1010. make_bad_inode(inode);
  1011. return;
  1012. }
  1013. udf_fill_inode(inode, bh);
  1014. brelse(bh);
  1015. }
  1016. static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
  1017. {
  1018. struct fileEntry *fe;
  1019. struct extendedFileEntry *efe;
  1020. int offset;
  1021. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1022. struct udf_inode_info *iinfo = UDF_I(inode);
  1023. fe = (struct fileEntry *)bh->b_data;
  1024. efe = (struct extendedFileEntry *)bh->b_data;
  1025. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1026. iinfo->i_strat4096 = 0;
  1027. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1028. iinfo->i_strat4096 = 1;
  1029. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1030. ICBTAG_FLAG_AD_MASK;
  1031. iinfo->i_unique = 0;
  1032. iinfo->i_lenEAttr = 0;
  1033. iinfo->i_lenExtents = 0;
  1034. iinfo->i_lenAlloc = 0;
  1035. iinfo->i_next_alloc_block = 0;
  1036. iinfo->i_next_alloc_goal = 0;
  1037. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1038. iinfo->i_efe = 1;
  1039. iinfo->i_use = 0;
  1040. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1041. sizeof(struct extendedFileEntry))) {
  1042. make_bad_inode(inode);
  1043. return;
  1044. }
  1045. memcpy(iinfo->i_ext.i_data,
  1046. bh->b_data + sizeof(struct extendedFileEntry),
  1047. inode->i_sb->s_blocksize -
  1048. sizeof(struct extendedFileEntry));
  1049. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1050. iinfo->i_efe = 0;
  1051. iinfo->i_use = 0;
  1052. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1053. sizeof(struct fileEntry))) {
  1054. make_bad_inode(inode);
  1055. return;
  1056. }
  1057. memcpy(iinfo->i_ext.i_data,
  1058. bh->b_data + sizeof(struct fileEntry),
  1059. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1060. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1061. iinfo->i_efe = 0;
  1062. iinfo->i_use = 1;
  1063. iinfo->i_lenAlloc = le32_to_cpu(
  1064. ((struct unallocSpaceEntry *)bh->b_data)->
  1065. lengthAllocDescs);
  1066. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1067. sizeof(struct unallocSpaceEntry))) {
  1068. make_bad_inode(inode);
  1069. return;
  1070. }
  1071. memcpy(iinfo->i_ext.i_data,
  1072. bh->b_data + sizeof(struct unallocSpaceEntry),
  1073. inode->i_sb->s_blocksize -
  1074. sizeof(struct unallocSpaceEntry));
  1075. return;
  1076. }
  1077. inode->i_uid = le32_to_cpu(fe->uid);
  1078. if (inode->i_uid == -1 ||
  1079. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
  1080. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1081. inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
  1082. inode->i_gid = le32_to_cpu(fe->gid);
  1083. if (inode->i_gid == -1 ||
  1084. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
  1085. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1086. inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
  1087. inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
  1088. if (!inode->i_nlink)
  1089. inode->i_nlink = 1;
  1090. inode->i_size = le64_to_cpu(fe->informationLength);
  1091. iinfo->i_lenExtents = inode->i_size;
  1092. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1093. sbi->s_fmode != UDF_INVALID_MODE)
  1094. inode->i_mode = sbi->s_fmode;
  1095. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1096. sbi->s_dmode != UDF_INVALID_MODE)
  1097. inode->i_mode = sbi->s_dmode;
  1098. else
  1099. inode->i_mode = udf_convert_permissions(fe);
  1100. inode->i_mode &= ~sbi->s_umask;
  1101. if (iinfo->i_efe == 0) {
  1102. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1103. (inode->i_sb->s_blocksize_bits - 9);
  1104. if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
  1105. inode->i_atime = sbi->s_record_time;
  1106. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1107. fe->modificationTime))
  1108. inode->i_mtime = sbi->s_record_time;
  1109. if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
  1110. inode->i_ctime = sbi->s_record_time;
  1111. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1112. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1113. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1114. offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
  1115. } else {
  1116. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1117. (inode->i_sb->s_blocksize_bits - 9);
  1118. if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
  1119. inode->i_atime = sbi->s_record_time;
  1120. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1121. efe->modificationTime))
  1122. inode->i_mtime = sbi->s_record_time;
  1123. if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
  1124. iinfo->i_crtime = sbi->s_record_time;
  1125. if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
  1126. inode->i_ctime = sbi->s_record_time;
  1127. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1128. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1129. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1130. offset = sizeof(struct extendedFileEntry) +
  1131. iinfo->i_lenEAttr;
  1132. }
  1133. switch (fe->icbTag.fileType) {
  1134. case ICBTAG_FILE_TYPE_DIRECTORY:
  1135. inode->i_op = &udf_dir_inode_operations;
  1136. inode->i_fop = &udf_dir_operations;
  1137. inode->i_mode |= S_IFDIR;
  1138. inc_nlink(inode);
  1139. break;
  1140. case ICBTAG_FILE_TYPE_REALTIME:
  1141. case ICBTAG_FILE_TYPE_REGULAR:
  1142. case ICBTAG_FILE_TYPE_UNDEF:
  1143. case ICBTAG_FILE_TYPE_VAT20:
  1144. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1145. inode->i_data.a_ops = &udf_adinicb_aops;
  1146. else
  1147. inode->i_data.a_ops = &udf_aops;
  1148. inode->i_op = &udf_file_inode_operations;
  1149. inode->i_fop = &udf_file_operations;
  1150. inode->i_mode |= S_IFREG;
  1151. break;
  1152. case ICBTAG_FILE_TYPE_BLOCK:
  1153. inode->i_mode |= S_IFBLK;
  1154. break;
  1155. case ICBTAG_FILE_TYPE_CHAR:
  1156. inode->i_mode |= S_IFCHR;
  1157. break;
  1158. case ICBTAG_FILE_TYPE_FIFO:
  1159. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1160. break;
  1161. case ICBTAG_FILE_TYPE_SOCKET:
  1162. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1163. break;
  1164. case ICBTAG_FILE_TYPE_SYMLINK:
  1165. inode->i_data.a_ops = &udf_symlink_aops;
  1166. inode->i_op = &page_symlink_inode_operations;
  1167. inode->i_mode = S_IFLNK | S_IRWXUGO;
  1168. break;
  1169. case ICBTAG_FILE_TYPE_MAIN:
  1170. udf_debug("METADATA FILE-----\n");
  1171. break;
  1172. case ICBTAG_FILE_TYPE_MIRROR:
  1173. udf_debug("METADATA MIRROR FILE-----\n");
  1174. break;
  1175. case ICBTAG_FILE_TYPE_BITMAP:
  1176. udf_debug("METADATA BITMAP FILE-----\n");
  1177. break;
  1178. default:
  1179. printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
  1180. "file type=%d\n", inode->i_ino,
  1181. fe->icbTag.fileType);
  1182. make_bad_inode(inode);
  1183. return;
  1184. }
  1185. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1186. struct deviceSpec *dsea =
  1187. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1188. if (dsea) {
  1189. init_special_inode(inode, inode->i_mode,
  1190. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1191. le32_to_cpu(dsea->minorDeviceIdent)));
  1192. /* Developer ID ??? */
  1193. } else
  1194. make_bad_inode(inode);
  1195. }
  1196. }
  1197. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1198. {
  1199. struct udf_inode_info *iinfo = UDF_I(inode);
  1200. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1201. if (!iinfo->i_ext.i_data) {
  1202. printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
  1203. "no free memory\n", inode->i_ino);
  1204. return -ENOMEM;
  1205. }
  1206. return 0;
  1207. }
  1208. static mode_t udf_convert_permissions(struct fileEntry *fe)
  1209. {
  1210. mode_t mode;
  1211. uint32_t permissions;
  1212. uint32_t flags;
  1213. permissions = le32_to_cpu(fe->permissions);
  1214. flags = le16_to_cpu(fe->icbTag.flags);
  1215. mode = ((permissions) & S_IRWXO) |
  1216. ((permissions >> 2) & S_IRWXG) |
  1217. ((permissions >> 4) & S_IRWXU) |
  1218. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1219. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1220. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1221. return mode;
  1222. }
  1223. int udf_write_inode(struct inode *inode, int sync)
  1224. {
  1225. int ret;
  1226. lock_kernel();
  1227. ret = udf_update_inode(inode, sync);
  1228. unlock_kernel();
  1229. return ret;
  1230. }
  1231. int udf_sync_inode(struct inode *inode)
  1232. {
  1233. return udf_update_inode(inode, 1);
  1234. }
  1235. static int udf_update_inode(struct inode *inode, int do_sync)
  1236. {
  1237. struct buffer_head *bh = NULL;
  1238. struct fileEntry *fe;
  1239. struct extendedFileEntry *efe;
  1240. uint32_t udfperms;
  1241. uint16_t icbflags;
  1242. uint16_t crclen;
  1243. int err = 0;
  1244. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1245. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1246. struct udf_inode_info *iinfo = UDF_I(inode);
  1247. bh = udf_tread(inode->i_sb,
  1248. udf_get_lb_pblock(inode->i_sb,
  1249. &iinfo->i_location, 0));
  1250. if (!bh) {
  1251. udf_debug("bread failure\n");
  1252. return -EIO;
  1253. }
  1254. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  1255. fe = (struct fileEntry *)bh->b_data;
  1256. efe = (struct extendedFileEntry *)bh->b_data;
  1257. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1258. struct unallocSpaceEntry *use =
  1259. (struct unallocSpaceEntry *)bh->b_data;
  1260. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1261. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1262. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1263. sizeof(struct unallocSpaceEntry));
  1264. crclen = sizeof(struct unallocSpaceEntry) +
  1265. iinfo->i_lenAlloc - sizeof(struct tag);
  1266. use->descTag.tagLocation = cpu_to_le32(
  1267. iinfo->i_location.
  1268. logicalBlockNum);
  1269. use->descTag.descCRCLength = cpu_to_le16(crclen);
  1270. use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
  1271. sizeof(struct tag),
  1272. crclen));
  1273. use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
  1274. mark_buffer_dirty(bh);
  1275. brelse(bh);
  1276. return err;
  1277. }
  1278. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1279. fe->uid = cpu_to_le32(-1);
  1280. else
  1281. fe->uid = cpu_to_le32(inode->i_uid);
  1282. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1283. fe->gid = cpu_to_le32(-1);
  1284. else
  1285. fe->gid = cpu_to_le32(inode->i_gid);
  1286. udfperms = ((inode->i_mode & S_IRWXO)) |
  1287. ((inode->i_mode & S_IRWXG) << 2) |
  1288. ((inode->i_mode & S_IRWXU) << 4);
  1289. udfperms |= (le32_to_cpu(fe->permissions) &
  1290. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1291. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1292. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1293. fe->permissions = cpu_to_le32(udfperms);
  1294. if (S_ISDIR(inode->i_mode))
  1295. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1296. else
  1297. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1298. fe->informationLength = cpu_to_le64(inode->i_size);
  1299. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1300. struct regid *eid;
  1301. struct deviceSpec *dsea =
  1302. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1303. if (!dsea) {
  1304. dsea = (struct deviceSpec *)
  1305. udf_add_extendedattr(inode,
  1306. sizeof(struct deviceSpec) +
  1307. sizeof(struct regid), 12, 0x3);
  1308. dsea->attrType = cpu_to_le32(12);
  1309. dsea->attrSubtype = 1;
  1310. dsea->attrLength = cpu_to_le32(
  1311. sizeof(struct deviceSpec) +
  1312. sizeof(struct regid));
  1313. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1314. }
  1315. eid = (struct regid *)dsea->impUse;
  1316. memset(eid, 0, sizeof(struct regid));
  1317. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1318. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1319. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1320. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1321. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1322. }
  1323. if (iinfo->i_efe == 0) {
  1324. memcpy(bh->b_data + sizeof(struct fileEntry),
  1325. iinfo->i_ext.i_data,
  1326. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1327. fe->logicalBlocksRecorded = cpu_to_le64(
  1328. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1329. (blocksize_bits - 9));
  1330. udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
  1331. udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
  1332. udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
  1333. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1334. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1335. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1336. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1337. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1338. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1339. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1340. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1341. crclen = sizeof(struct fileEntry);
  1342. } else {
  1343. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1344. iinfo->i_ext.i_data,
  1345. inode->i_sb->s_blocksize -
  1346. sizeof(struct extendedFileEntry));
  1347. efe->objectSize = cpu_to_le64(inode->i_size);
  1348. efe->logicalBlocksRecorded = cpu_to_le64(
  1349. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1350. (blocksize_bits - 9));
  1351. if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
  1352. (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
  1353. iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
  1354. iinfo->i_crtime = inode->i_atime;
  1355. if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
  1356. (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
  1357. iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
  1358. iinfo->i_crtime = inode->i_mtime;
  1359. if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
  1360. (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
  1361. iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
  1362. iinfo->i_crtime = inode->i_ctime;
  1363. udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
  1364. udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
  1365. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1366. udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
  1367. memset(&(efe->impIdent), 0, sizeof(struct regid));
  1368. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1369. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1370. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1371. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1372. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1373. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1374. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1375. crclen = sizeof(struct extendedFileEntry);
  1376. }
  1377. if (iinfo->i_strat4096) {
  1378. fe->icbTag.strategyType = cpu_to_le16(4096);
  1379. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1380. fe->icbTag.numEntries = cpu_to_le16(2);
  1381. } else {
  1382. fe->icbTag.strategyType = cpu_to_le16(4);
  1383. fe->icbTag.numEntries = cpu_to_le16(1);
  1384. }
  1385. if (S_ISDIR(inode->i_mode))
  1386. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1387. else if (S_ISREG(inode->i_mode))
  1388. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1389. else if (S_ISLNK(inode->i_mode))
  1390. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1391. else if (S_ISBLK(inode->i_mode))
  1392. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1393. else if (S_ISCHR(inode->i_mode))
  1394. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1395. else if (S_ISFIFO(inode->i_mode))
  1396. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1397. else if (S_ISSOCK(inode->i_mode))
  1398. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1399. icbflags = iinfo->i_alloc_type |
  1400. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1401. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1402. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1403. (le16_to_cpu(fe->icbTag.flags) &
  1404. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1405. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1406. fe->icbTag.flags = cpu_to_le16(icbflags);
  1407. if (sbi->s_udfrev >= 0x0200)
  1408. fe->descTag.descVersion = cpu_to_le16(3);
  1409. else
  1410. fe->descTag.descVersion = cpu_to_le16(2);
  1411. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1412. fe->descTag.tagLocation = cpu_to_le32(
  1413. iinfo->i_location.logicalBlockNum);
  1414. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
  1415. sizeof(struct tag);
  1416. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1417. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1418. crclen));
  1419. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1420. /* write the data blocks */
  1421. mark_buffer_dirty(bh);
  1422. if (do_sync) {
  1423. sync_dirty_buffer(bh);
  1424. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1425. printk(KERN_WARNING "IO error syncing udf inode "
  1426. "[%s:%08lx]\n", inode->i_sb->s_id,
  1427. inode->i_ino);
  1428. err = -EIO;
  1429. }
  1430. }
  1431. brelse(bh);
  1432. return err;
  1433. }
  1434. struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
  1435. {
  1436. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1437. struct inode *inode = iget_locked(sb, block);
  1438. if (!inode)
  1439. return NULL;
  1440. if (inode->i_state & I_NEW) {
  1441. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1442. __udf_read_inode(inode);
  1443. unlock_new_inode(inode);
  1444. }
  1445. if (is_bad_inode(inode))
  1446. goto out_iput;
  1447. if (ino->logicalBlockNum >= UDF_SB(sb)->
  1448. s_partmaps[ino->partitionReferenceNum].s_partition_len) {
  1449. udf_debug("block=%d, partition=%d out of range\n",
  1450. ino->logicalBlockNum, ino->partitionReferenceNum);
  1451. make_bad_inode(inode);
  1452. goto out_iput;
  1453. }
  1454. return inode;
  1455. out_iput:
  1456. iput(inode);
  1457. return NULL;
  1458. }
  1459. int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
  1460. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1461. {
  1462. int adsize;
  1463. struct short_ad *sad = NULL;
  1464. struct long_ad *lad = NULL;
  1465. struct allocExtDesc *aed;
  1466. int8_t etype;
  1467. uint8_t *ptr;
  1468. struct udf_inode_info *iinfo = UDF_I(inode);
  1469. if (!epos->bh)
  1470. ptr = iinfo->i_ext.i_data + epos->offset -
  1471. udf_file_entry_alloc_offset(inode) +
  1472. iinfo->i_lenEAttr;
  1473. else
  1474. ptr = epos->bh->b_data + epos->offset;
  1475. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1476. adsize = sizeof(struct short_ad);
  1477. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1478. adsize = sizeof(struct long_ad);
  1479. else
  1480. return -1;
  1481. if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
  1482. char *sptr, *dptr;
  1483. struct buffer_head *nbh;
  1484. int err, loffset;
  1485. struct kernel_lb_addr obloc = epos->block;
  1486. epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
  1487. obloc.partitionReferenceNum,
  1488. obloc.logicalBlockNum, &err);
  1489. if (!epos->block.logicalBlockNum)
  1490. return -1;
  1491. nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
  1492. &epos->block,
  1493. 0));
  1494. if (!nbh)
  1495. return -1;
  1496. lock_buffer(nbh);
  1497. memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
  1498. set_buffer_uptodate(nbh);
  1499. unlock_buffer(nbh);
  1500. mark_buffer_dirty_inode(nbh, inode);
  1501. aed = (struct allocExtDesc *)(nbh->b_data);
  1502. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  1503. aed->previousAllocExtLocation =
  1504. cpu_to_le32(obloc.logicalBlockNum);
  1505. if (epos->offset + adsize > inode->i_sb->s_blocksize) {
  1506. loffset = epos->offset;
  1507. aed->lengthAllocDescs = cpu_to_le32(adsize);
  1508. sptr = ptr - adsize;
  1509. dptr = nbh->b_data + sizeof(struct allocExtDesc);
  1510. memcpy(dptr, sptr, adsize);
  1511. epos->offset = sizeof(struct allocExtDesc) + adsize;
  1512. } else {
  1513. loffset = epos->offset + adsize;
  1514. aed->lengthAllocDescs = cpu_to_le32(0);
  1515. sptr = ptr;
  1516. epos->offset = sizeof(struct allocExtDesc);
  1517. if (epos->bh) {
  1518. aed = (struct allocExtDesc *)epos->bh->b_data;
  1519. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1520. } else {
  1521. iinfo->i_lenAlloc += adsize;
  1522. mark_inode_dirty(inode);
  1523. }
  1524. }
  1525. if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
  1526. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
  1527. epos->block.logicalBlockNum, sizeof(struct tag));
  1528. else
  1529. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
  1530. epos->block.logicalBlockNum, sizeof(struct tag));
  1531. switch (iinfo->i_alloc_type) {
  1532. case ICBTAG_FLAG_AD_SHORT:
  1533. sad = (struct short_ad *)sptr;
  1534. sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1535. inode->i_sb->s_blocksize);
  1536. sad->extPosition =
  1537. cpu_to_le32(epos->block.logicalBlockNum);
  1538. break;
  1539. case ICBTAG_FLAG_AD_LONG:
  1540. lad = (struct long_ad *)sptr;
  1541. lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1542. inode->i_sb->s_blocksize);
  1543. lad->extLocation = cpu_to_lelb(epos->block);
  1544. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1545. break;
  1546. }
  1547. if (epos->bh) {
  1548. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1549. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1550. udf_update_tag(epos->bh->b_data, loffset);
  1551. else
  1552. udf_update_tag(epos->bh->b_data,
  1553. sizeof(struct allocExtDesc));
  1554. mark_buffer_dirty_inode(epos->bh, inode);
  1555. brelse(epos->bh);
  1556. } else {
  1557. mark_inode_dirty(inode);
  1558. }
  1559. epos->bh = nbh;
  1560. }
  1561. etype = udf_write_aext(inode, epos, eloc, elen, inc);
  1562. if (!epos->bh) {
  1563. iinfo->i_lenAlloc += adsize;
  1564. mark_inode_dirty(inode);
  1565. } else {
  1566. aed = (struct allocExtDesc *)epos->bh->b_data;
  1567. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1568. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1569. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1570. udf_update_tag(epos->bh->b_data,
  1571. epos->offset + (inc ? 0 : adsize));
  1572. else
  1573. udf_update_tag(epos->bh->b_data,
  1574. sizeof(struct allocExtDesc));
  1575. mark_buffer_dirty_inode(epos->bh, inode);
  1576. }
  1577. return etype;
  1578. }
  1579. int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
  1580. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1581. {
  1582. int adsize;
  1583. uint8_t *ptr;
  1584. struct short_ad *sad;
  1585. struct long_ad *lad;
  1586. struct udf_inode_info *iinfo = UDF_I(inode);
  1587. if (!epos->bh)
  1588. ptr = iinfo->i_ext.i_data + epos->offset -
  1589. udf_file_entry_alloc_offset(inode) +
  1590. iinfo->i_lenEAttr;
  1591. else
  1592. ptr = epos->bh->b_data + epos->offset;
  1593. switch (iinfo->i_alloc_type) {
  1594. case ICBTAG_FLAG_AD_SHORT:
  1595. sad = (struct short_ad *)ptr;
  1596. sad->extLength = cpu_to_le32(elen);
  1597. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1598. adsize = sizeof(struct short_ad);
  1599. break;
  1600. case ICBTAG_FLAG_AD_LONG:
  1601. lad = (struct long_ad *)ptr;
  1602. lad->extLength = cpu_to_le32(elen);
  1603. lad->extLocation = cpu_to_lelb(*eloc);
  1604. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1605. adsize = sizeof(struct long_ad);
  1606. break;
  1607. default:
  1608. return -1;
  1609. }
  1610. if (epos->bh) {
  1611. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1612. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1613. struct allocExtDesc *aed =
  1614. (struct allocExtDesc *)epos->bh->b_data;
  1615. udf_update_tag(epos->bh->b_data,
  1616. le32_to_cpu(aed->lengthAllocDescs) +
  1617. sizeof(struct allocExtDesc));
  1618. }
  1619. mark_buffer_dirty_inode(epos->bh, inode);
  1620. } else {
  1621. mark_inode_dirty(inode);
  1622. }
  1623. if (inc)
  1624. epos->offset += adsize;
  1625. return (elen >> 30);
  1626. }
  1627. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1628. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1629. {
  1630. int8_t etype;
  1631. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1632. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1633. int block;
  1634. epos->block = *eloc;
  1635. epos->offset = sizeof(struct allocExtDesc);
  1636. brelse(epos->bh);
  1637. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1638. epos->bh = udf_tread(inode->i_sb, block);
  1639. if (!epos->bh) {
  1640. udf_debug("reading block %d failed!\n", block);
  1641. return -1;
  1642. }
  1643. }
  1644. return etype;
  1645. }
  1646. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1647. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1648. {
  1649. int alen;
  1650. int8_t etype;
  1651. uint8_t *ptr;
  1652. struct short_ad *sad;
  1653. struct long_ad *lad;
  1654. struct udf_inode_info *iinfo = UDF_I(inode);
  1655. if (!epos->bh) {
  1656. if (!epos->offset)
  1657. epos->offset = udf_file_entry_alloc_offset(inode);
  1658. ptr = iinfo->i_ext.i_data + epos->offset -
  1659. udf_file_entry_alloc_offset(inode) +
  1660. iinfo->i_lenEAttr;
  1661. alen = udf_file_entry_alloc_offset(inode) +
  1662. iinfo->i_lenAlloc;
  1663. } else {
  1664. if (!epos->offset)
  1665. epos->offset = sizeof(struct allocExtDesc);
  1666. ptr = epos->bh->b_data + epos->offset;
  1667. alen = sizeof(struct allocExtDesc) +
  1668. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1669. lengthAllocDescs);
  1670. }
  1671. switch (iinfo->i_alloc_type) {
  1672. case ICBTAG_FLAG_AD_SHORT:
  1673. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1674. if (!sad)
  1675. return -1;
  1676. etype = le32_to_cpu(sad->extLength) >> 30;
  1677. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1678. eloc->partitionReferenceNum =
  1679. iinfo->i_location.partitionReferenceNum;
  1680. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1681. break;
  1682. case ICBTAG_FLAG_AD_LONG:
  1683. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1684. if (!lad)
  1685. return -1;
  1686. etype = le32_to_cpu(lad->extLength) >> 30;
  1687. *eloc = lelb_to_cpu(lad->extLocation);
  1688. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1689. break;
  1690. default:
  1691. udf_debug("alloc_type = %d unsupported\n",
  1692. iinfo->i_alloc_type);
  1693. return -1;
  1694. }
  1695. return etype;
  1696. }
  1697. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1698. struct kernel_lb_addr neloc, uint32_t nelen)
  1699. {
  1700. struct kernel_lb_addr oeloc;
  1701. uint32_t oelen;
  1702. int8_t etype;
  1703. if (epos.bh)
  1704. get_bh(epos.bh);
  1705. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1706. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1707. neloc = oeloc;
  1708. nelen = (etype << 30) | oelen;
  1709. }
  1710. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1711. brelse(epos.bh);
  1712. return (nelen >> 30);
  1713. }
  1714. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1715. struct kernel_lb_addr eloc, uint32_t elen)
  1716. {
  1717. struct extent_position oepos;
  1718. int adsize;
  1719. int8_t etype;
  1720. struct allocExtDesc *aed;
  1721. struct udf_inode_info *iinfo;
  1722. if (epos.bh) {
  1723. get_bh(epos.bh);
  1724. get_bh(epos.bh);
  1725. }
  1726. iinfo = UDF_I(inode);
  1727. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1728. adsize = sizeof(struct short_ad);
  1729. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1730. adsize = sizeof(struct long_ad);
  1731. else
  1732. adsize = 0;
  1733. oepos = epos;
  1734. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1735. return -1;
  1736. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1737. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1738. if (oepos.bh != epos.bh) {
  1739. oepos.block = epos.block;
  1740. brelse(oepos.bh);
  1741. get_bh(epos.bh);
  1742. oepos.bh = epos.bh;
  1743. oepos.offset = epos.offset - adsize;
  1744. }
  1745. }
  1746. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1747. elen = 0;
  1748. if (epos.bh != oepos.bh) {
  1749. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1750. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1751. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1752. if (!oepos.bh) {
  1753. iinfo->i_lenAlloc -= (adsize * 2);
  1754. mark_inode_dirty(inode);
  1755. } else {
  1756. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1757. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1758. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1759. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1760. udf_update_tag(oepos.bh->b_data,
  1761. oepos.offset - (2 * adsize));
  1762. else
  1763. udf_update_tag(oepos.bh->b_data,
  1764. sizeof(struct allocExtDesc));
  1765. mark_buffer_dirty_inode(oepos.bh, inode);
  1766. }
  1767. } else {
  1768. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1769. if (!oepos.bh) {
  1770. iinfo->i_lenAlloc -= adsize;
  1771. mark_inode_dirty(inode);
  1772. } else {
  1773. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1774. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  1775. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1776. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1777. udf_update_tag(oepos.bh->b_data,
  1778. epos.offset - adsize);
  1779. else
  1780. udf_update_tag(oepos.bh->b_data,
  1781. sizeof(struct allocExtDesc));
  1782. mark_buffer_dirty_inode(oepos.bh, inode);
  1783. }
  1784. }
  1785. brelse(epos.bh);
  1786. brelse(oepos.bh);
  1787. return (elen >> 30);
  1788. }
  1789. int8_t inode_bmap(struct inode *inode, sector_t block,
  1790. struct extent_position *pos, struct kernel_lb_addr *eloc,
  1791. uint32_t *elen, sector_t *offset)
  1792. {
  1793. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1794. loff_t lbcount = 0, bcount =
  1795. (loff_t) block << blocksize_bits;
  1796. int8_t etype;
  1797. struct udf_inode_info *iinfo;
  1798. iinfo = UDF_I(inode);
  1799. pos->offset = 0;
  1800. pos->block = iinfo->i_location;
  1801. pos->bh = NULL;
  1802. *elen = 0;
  1803. do {
  1804. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  1805. if (etype == -1) {
  1806. *offset = (bcount - lbcount) >> blocksize_bits;
  1807. iinfo->i_lenExtents = lbcount;
  1808. return -1;
  1809. }
  1810. lbcount += *elen;
  1811. } while (lbcount <= bcount);
  1812. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  1813. return etype;
  1814. }
  1815. long udf_block_map(struct inode *inode, sector_t block)
  1816. {
  1817. struct kernel_lb_addr eloc;
  1818. uint32_t elen;
  1819. sector_t offset;
  1820. struct extent_position epos = {};
  1821. int ret;
  1822. lock_kernel();
  1823. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  1824. (EXT_RECORDED_ALLOCATED >> 30))
  1825. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  1826. else
  1827. ret = 0;
  1828. unlock_kernel();
  1829. brelse(epos.bh);
  1830. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  1831. return udf_fixed_to_variable(ret);
  1832. else
  1833. return ret;
  1834. }