inode.c 61 KB

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