Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/ufs/util.c
0004  *
0005  * Copyright (C) 1998
0006  * Daniel Pirkl <daniel.pirkl@email.cz>
0007  * Charles University, Faculty of Mathematics and Physics
0008  */
0009  
0010 #include <linux/string.h>
0011 #include <linux/slab.h>
0012 #include <linux/buffer_head.h>
0013 
0014 #include "ufs_fs.h"
0015 #include "ufs.h"
0016 #include "swab.h"
0017 #include "util.h"
0018 
0019 struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi,
0020     struct super_block *sb, u64 fragment, u64 size)
0021 {
0022     struct ufs_buffer_head * ubh;
0023     unsigned i, j ;
0024     u64  count = 0;
0025     if (size & ~uspi->s_fmask)
0026         return NULL;
0027     count = size >> uspi->s_fshift;
0028     if (count > UFS_MAXFRAG)
0029         return NULL;
0030     ubh = kmalloc (sizeof (struct ufs_buffer_head), GFP_NOFS);
0031     if (!ubh)
0032         return NULL;
0033     ubh->fragment = fragment;
0034     ubh->count = count;
0035     for (i = 0; i < count; i++)
0036         if (!(ubh->bh[i] = sb_bread(sb, fragment + i)))
0037             goto failed;
0038     for (; i < UFS_MAXFRAG; i++)
0039         ubh->bh[i] = NULL;
0040     return ubh;
0041 failed:
0042     for (j = 0; j < i; j++)
0043         brelse (ubh->bh[j]);
0044     kfree(ubh);
0045     return NULL;
0046 }
0047 
0048 struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi,
0049     struct super_block *sb, u64 fragment, u64 size)
0050 {
0051     unsigned i, j;
0052     u64 count = 0;
0053     if (size & ~uspi->s_fmask)
0054         return NULL;
0055     count = size >> uspi->s_fshift;
0056     if (count <= 0 || count > UFS_MAXFRAG)
0057         return NULL;
0058     USPI_UBH(uspi)->fragment = fragment;
0059     USPI_UBH(uspi)->count = count;
0060     for (i = 0; i < count; i++)
0061         if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i)))
0062             goto failed;
0063     for (; i < UFS_MAXFRAG; i++)
0064         USPI_UBH(uspi)->bh[i] = NULL;
0065     return USPI_UBH(uspi);
0066 failed:
0067     for (j = 0; j < i; j++)
0068         brelse (USPI_UBH(uspi)->bh[j]);
0069     return NULL;
0070 }
0071 
0072 void ubh_brelse (struct ufs_buffer_head * ubh)
0073 {
0074     unsigned i;
0075     if (!ubh)
0076         return;
0077     for (i = 0; i < ubh->count; i++)
0078         brelse (ubh->bh[i]);
0079     kfree (ubh);
0080 }
0081 
0082 void ubh_brelse_uspi (struct ufs_sb_private_info * uspi)
0083 {
0084     unsigned i;
0085     if (!USPI_UBH(uspi))
0086         return;
0087     for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) {
0088         brelse (USPI_UBH(uspi)->bh[i]);
0089         USPI_UBH(uspi)->bh[i] = NULL;
0090     }
0091 }
0092 
0093 void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh)
0094 {
0095     unsigned i;
0096     if (!ubh)
0097         return;
0098     for ( i = 0; i < ubh->count; i++ )
0099         mark_buffer_dirty (ubh->bh[i]);
0100 }
0101 
0102 void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag)
0103 {
0104     unsigned i;
0105     if (!ubh)
0106         return;
0107     if (flag) {
0108         for ( i = 0; i < ubh->count; i++ )
0109             set_buffer_uptodate (ubh->bh[i]);
0110     } else {
0111         for ( i = 0; i < ubh->count; i++ )
0112             clear_buffer_uptodate (ubh->bh[i]);
0113     }
0114 }
0115 
0116 void ubh_sync_block(struct ufs_buffer_head *ubh)
0117 {
0118     if (ubh) {
0119         unsigned i;
0120 
0121         for (i = 0; i < ubh->count; i++)
0122             write_dirty_buffer(ubh->bh[i], 0);
0123 
0124         for (i = 0; i < ubh->count; i++)
0125             wait_on_buffer(ubh->bh[i]);
0126     }
0127 }
0128 
0129 void ubh_bforget (struct ufs_buffer_head * ubh)
0130 {
0131     unsigned i;
0132     if (!ubh) 
0133         return;
0134     for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] ) 
0135         bforget (ubh->bh[i]);
0136 }
0137  
0138 int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
0139 {
0140     unsigned i;
0141     unsigned result = 0;
0142     if (!ubh)
0143         return 0;
0144     for ( i = 0; i < ubh->count; i++ )
0145         result |= buffer_dirty(ubh->bh[i]);
0146     return result;
0147 }
0148 
0149 void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi, 
0150     unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
0151 {
0152     unsigned len, bhno;
0153     if (size > (ubh->count << uspi->s_fshift))
0154         size = ubh->count << uspi->s_fshift;
0155     bhno = 0;
0156     while (size) {
0157         len = min_t(unsigned int, size, uspi->s_fsize);
0158         memcpy (mem, ubh->bh[bhno]->b_data, len);
0159         mem += uspi->s_fsize;
0160         size -= len;
0161         bhno++;
0162     }
0163 }
0164 
0165 void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi, 
0166     struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
0167 {
0168     unsigned len, bhno;
0169     if (size > (ubh->count << uspi->s_fshift))
0170         size = ubh->count << uspi->s_fshift;
0171     bhno = 0;
0172     while (size) {
0173         len = min_t(unsigned int, size, uspi->s_fsize);
0174         memcpy (ubh->bh[bhno]->b_data, mem, len);
0175         mem += uspi->s_fsize;
0176         size -= len;
0177         bhno++;
0178     }
0179 }
0180 
0181 dev_t
0182 ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
0183 {
0184     __u32 fs32;
0185     dev_t dev;
0186 
0187     if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
0188         fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[1]);
0189     else
0190         fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[0]);
0191     switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
0192     case UFS_ST_SUNx86:
0193     case UFS_ST_SUN:
0194         if ((fs32 & 0xffff0000) == 0 ||
0195             (fs32 & 0xffff0000) == 0xffff0000)
0196             dev = old_decode_dev(fs32 & 0x7fff);
0197         else
0198             dev = MKDEV(sysv_major(fs32), sysv_minor(fs32));
0199         break;
0200 
0201     default:
0202         dev = old_decode_dev(fs32);
0203         break;
0204     }
0205     return dev;
0206 }
0207 
0208 void
0209 ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev)
0210 {
0211     __u32 fs32;
0212 
0213     switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
0214     case UFS_ST_SUNx86:
0215     case UFS_ST_SUN:
0216         fs32 = sysv_encode_dev(dev);
0217         if ((fs32 & 0xffff8000) == 0) {
0218             fs32 = old_encode_dev(dev);
0219         }
0220         break;
0221 
0222     default:
0223         fs32 = old_encode_dev(dev);
0224         break;
0225     }
0226     if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
0227         ufsi->i_u1.i_data[1] = cpu_to_fs32(sb, fs32);
0228     else
0229         ufsi->i_u1.i_data[0] = cpu_to_fs32(sb, fs32);
0230 }
0231 
0232 /**
0233  * ufs_get_locked_page() - locate, pin and lock a pagecache page, if not exist
0234  * read it from disk.
0235  * @mapping: the address_space to search
0236  * @index: the page index
0237  *
0238  * Locates the desired pagecache page, if not exist we'll read it,
0239  * locks it, increments its reference
0240  * count and returns its address.
0241  *
0242  */
0243 
0244 struct page *ufs_get_locked_page(struct address_space *mapping,
0245                  pgoff_t index)
0246 {
0247     struct inode *inode = mapping->host;
0248     struct page *page = find_lock_page(mapping, index);
0249     if (!page) {
0250         page = read_mapping_page(mapping, index, NULL);
0251 
0252         if (IS_ERR(page)) {
0253             printk(KERN_ERR "ufs_change_blocknr: "
0254                    "read_mapping_page error: ino %lu, index: %lu\n",
0255                    mapping->host->i_ino, index);
0256             return page;
0257         }
0258 
0259         lock_page(page);
0260 
0261         if (unlikely(page->mapping == NULL)) {
0262             /* Truncate got there first */
0263             unlock_page(page);
0264             put_page(page);
0265             return NULL;
0266         }
0267     }
0268     if (!page_has_buffers(page))
0269         create_empty_buffers(page, 1 << inode->i_blkbits, 0);
0270     return page;
0271 }