inode.c 61 KB

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