inode.c 61 KB

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